Menu
CoddyTech

Blocken

Lektion 12 von 25 im Kurs Roblox-Spiel: Kampfarenen von Coddy.

F-Blöcke: Für Settings.BlockTime Sekunden verursacht jeder Treffer, den du einsteckst, nur einen Anteil seines Schadens, Settings.BlockMultiplier. Ob jemand blockt, ist ein Attribut seines Charakters, Blocking, sodass jeder es auslesen kann.

Da jeder Treffer durch hurt geht, wird die Regel nur einmal geschrieben:

if character:GetAttribute("Blocking") then
	amount *= Settings.BlockMultiplier
end

In Kapitel 4 werden Bots dich treffen, und dieselben Zeilen schützen dich dann.

challenge icon

Aufgabe

Mittel

Mit F blocken: Für einen Moment verursacht ein Treffer nur ein Fünftel Schaden.

  1. Füge abilities.Block(player, character) hinzu: Setze das Blocking-Attribut des Charakters auf true und setze es nach Settings.BlockTime zurück, falls der Charakter noch da ist.
  2. Multipliziere in hurt, solange der Charakter Blocking ist, amount mit Settings.BlockMultiplier.
  3. Binde in BattleClient "Block" an F und füge es zu shown hinzu.

Drücke „Spielen“ und dann F: Die Blockanzeige zählt herunter. Drücke anschließend „Prüfen“.

Probier es selbst

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)

local remotes = game.ReplicatedStorage.Remotes
local lastSwing = {} -- player -> wann zuletzt zugeschlagen wurde

-- Roblox' eigene Schwertanimationen
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"

print("Swords deal " .. Settings.SwordDamage .. " damage")

-- Jeder Treffer im Spiel wird hier verarbeitet
local function hurt(humanoid, amount)
	if humanoid.Health <= 0 then
		return
	end
	local character = humanoid.Parent
	humanoid:TakeDamage(amount)
end

-- Ein Schwertschlag: Zuerst kommt die Animation, dann werden alle in der Box vor dir getroffen
local function swing(player)
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if not root or character.Humanoid.Health <= 0 then
		return
	end
	local now = time()
	if lastSwing[player] and now - lastSwing[player] < Settings.SwingCooldown then
		return
	end
	lastSwing[player] = now
	character.Humanoid.Animator:LoadAnimation(slash):Play()
	-- die Box vor dir, niemals dein eigener Körper
	local params = OverlapParams.new()
	params.FilterDescendantsInstances = { character }
	local box = root.CFrame * CFrame.new(0, 0, -3)
	local hit = {} -- jeder humanoid nur einmal pro Schwertschlag
	for _, part in workspace:GetPartBoundsInBox(box, Settings.HitboxSize, params) do
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		if humanoid and not hit[humanoid] then
			hit[humanoid] = true
			hurt(humanoid, Settings.SwordDamage)
		end
	end
end

-- Die Fähigkeiten, nach Namen geordnet: was jede einzelne bewirkt
local abilities = {}

-- Das Gerät eines Spielers bewegt dessen Charakter, daher findet der Dash dort statt
function abilities.Dash(player)
	remotes.Ability:FireClient(player, "Dash")
end

-- Alle um dich herum werden getroffen und von dir weggeschleudert
function abilities.Slam(player, character, root)
	local params = OverlapParams.new()
	params.FilterDescendantsInstances = { character }
	local hit = {}
	for _, part in workspace:GetPartBoundsInRadius(root.Position, Settings.SlamRadius, params) do
		local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")
		local theirRoot = part.Parent:FindFirstChild("HumanoidRootPart")
		if humanoid and theirRoot and not hit[humanoid] then
			hit[humanoid] = true
			hurt(humanoid, Settings.SlamDamage)
			local away = (theirRoot.Position - root.Position) * Vector3.new(1, 0, 1)
			if away.Magnitude > 0 then
				away = away.Unit
			end
			theirRoot:ApplyImpulse((away * Settings.SlamKnockback + Vector3.new(0, 40, 0)) * theirRoot.AssemblyMass)
		end
	end
end

-- Lektion 12: Block kommt hierhin

-- Der Client fordert eine Fähigkeit anhand ihres Namens an. Der Server prüft: eine echte
-- Fähigkeit, ein lebender Spieler und eine abgelaufene Abklingzeit. Wann sie wieder bereit ist
-- ist ein Attribut des Spielers, damit es auf seinem Bildschirm angezeigt werden kann.
remotes.Ability.OnServerEvent:Connect(function(player, name)
	if typeof(name) ~= "string" or not abilities[name] then
		return
	end
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if not root or character.Humanoid.Health <= 0 then
		return
	end
	local now = workspace:GetServerTimeNow()
	if now < (player:GetAttribute(name .. "ReadyAt") or 0) then
		return
	end
	player:SetAttribute(name .. "ReadyAt", now + Settings.Cooldowns[name])
	abilities[name](player, character, root)
end)

-- Jeder Spieler bekommt bei jedem neuen Leben ein Schwert
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local sword = ServerStorage.Sword:Clone()
		sword.Parent = player.Backpack
		sword.Activated:Connect(function()
			swing(player)
		end)
	end)
end)

-- Die Trainingspuppe: Ihre Gesundheitsanzeige zeigt ihre Gesundheit an, und eine besiegte
-- Puppe erscheint nach kurzer Zeit als neue Kopie am selben Ort wieder
local function setupDummy(dummy)
	local humanoid = dummy.Humanoid
	humanoid.HealthChanged:Connect(function(health)
		dummy.HealthBar.Back.Fill.Size = UDim2.fromScale(health / humanoid.MaxHealth, 1)
	end)
	humanoid.Died:Connect(function()
		local spot = dummy:GetPivot()
		task.wait(0.5)
		dummy:Destroy()
		task.wait(Settings.DummyRespawn)
		local fresh = ServerStorage.Dummy:Clone()
		fresh:PivotTo(spot)
		fresh.Parent = workspace
		setupDummy(fresh)
	end)
end
setupDummy(workspace.Dummy)

Alle Lektionen in Roblox-Spiel: Kampfarenen

Übe selbstständig: Roblox-Playground