Menu
CoddyTech

זינוק בלחיצה על Q

שיעור 7 מתוך 25 בקורס משחק Roblox: זירות קרב של Coddy.

זינוק הוא דחיפה: הגדרת AssemblyLinearVelocity של שורש הדמות מעיפה אותה. אבל Roblox מאפשר לכל מכשיר של שחקן להריץ את הפיזיקה של הדמות שלו, ולכן דחיפה שהשרת מגדיר כמעט לא מזיזה אותה. השרת מקבל את ההחלטה, וה־LocalScript של השחקן עצמו דוחף.

השרת שומר את היכולות שלו בטבלה, לפי שם, ואישור לזינוק נשלח בחזרה ללקוח של אותו שחקן באמצעות FireClient, ושם מתקבל דרך OnClientEvent:

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

אתגר

בינוני

גרמו ל-Q לבצע זינוק: השרת מאשר, והמכשיר שלכם דוחף.

  1. ב-BattleServer, החליפו את ההדפסה בטבלת abilities: abilities.Dash(player) קוראת ל-remotes.Ability:FireClient(player, "Dash").
  2. המטפל מפעיל את abilities[name](player, character, root) כשהיא קיימת.
  3. ב-BattleClient, בעת remotes.Ability.OnClientEvent עם "Dash", הגדירו את המהירות של השורש שלכם ל-root.CFrame.LookVector * Settings.DashSpeed + Vector3.new(0, 15, 0).

לחצו על "שחק" ולחצו על Q: תזנקו קדימה. לאחר מכן לחצו על "בדיקה".

נסו בעצמכם

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

local remotes = game.ReplicatedStorage.Remotes
local lastSwing = {} -- player -> מתי הניף את החרב בפעם האחרונה

-- הנפשות החרב של Roblox עצמה
local slash = Instance.new("Animation")
slash.AnimationId = "rbxassetid://522635514"

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

-- הנפת חרב: ההנפשה מתנגנת, ואז כל מי שנמצא בתיבה שלפניך נפגע
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()
	-- התיבה שלפניך, לעולם לא הגוף שלך
	local params = OverlapParams.new()
	params.FilterDescendantsInstances = { character }
	local box = root.CFrame * CFrame.new(0, 0, -3)
	local hit = {} -- כל humanoid פעם אחת בכל הנפת חרב
	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

-- הלקוח מבקש יכולת לפי שמה
remotes.Ability.OnServerEvent:Connect(function(player, name)
	print(player.Name .. " asked for " .. name)
end)

-- כל שחקן מקבל חרב בכל פעם שהוא חוזר לחיים
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)

-- בובת האימון: מד הבריאות שלה משקף את מצב הבריאות שלה, ובובה שהובסה
-- חוזרת כעבור רגע, כעותק חדש באותו המקום
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)

כל השיעורים ביחידה משחק Roblox: זירות קרב

תרגלו בעצמכם: Roblox אונליין