Menu
CoddyTech

Dash bei Q

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

Ein Satz ist ein Stoß: Wenn du die AssemblyLinearVelocity einer Wurzel festlegst, schleudert das die Figur weg. Roblox lässt jedoch das Gerät jedes Spielers die Physik der eigenen Figur berechnen, daher bewegt ein Stoß, den der Server festlegt, die Figur kaum. Der Server entscheidet, und das eigene LocalScript des Spielers stößt sie an.

Der Server speichert seine Fähigkeiten in einer Tabelle, nach Namen, und ein Ja zu einem Satz geht mit FireClient zurück an den Client dieses Spielers, wo es von OnClientEvent empfangen wird:

remotes.Ability.OnClientEvent:Connect(function(name)
	if name == "Hop" then
		root.AssemblyLinearVelocity = Vector3.new(0, 50, 0)
	end
end)
challenge icon

Aufgabe

Mittel

Erstelle den Q-Sprint: Der Server gibt grünes Licht, dein Gerät stößt dich an.

  1. Ersetze in BattleServer die Ausgabe durch eine Tabelle abilities: abilities.Dash(player) ruft remotes.Ability:FireClient(player, "Dash") auf.
  2. Der Handler führt abilities[name](player, character, root) aus, wenn es existiert.
  3. Setze in BattleClient bei remotes.Ability.OnClientEvent mit "Dash" die Geschwindigkeit deiner Wurzel auf root.CFrame.LookVector * Settings.DashSpeed + Vector3.new(0, 15, 0).

Drücke „Spielen“ und dann Q: Du sprintest nach vorn. Danach „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 er zuletzt zugeschlagen hat

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

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

-- Ein Schwerthieb: erst 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 einmal pro Schwerthieb
	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
			humanoid:TakeDamage(Settings.SwordDamage)
			print(humanoid.Parent.Name .. " is at " .. humanoid.Health)
		end
	end
end

-- Der Client fragt nach einer Fähigkeit anhand ihres Namens
remotes.Ability.OnServerEvent:Connect(function(player, name)
	print(player.Name .. " asked for " .. name)
end)

-- Jeder Spieler erhält 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