דאש בלחיצה על Q
שיעור 6 מתוך 20 בקורס משחק Roblox: זירות קרב של Coddy.
היכולות מתחילות במכשיר של השחקן. BattleClient הוא LocalScript בתוך ה-HUD, ו-ContextActionService מקשר פעולה למקשים, ועם ארגומנט שלישי true גם יוצר עבורה כפתור במסך הטלפון:
ContextActionService:BindAction("Dash", use, true, Enum.KeyCode.Q)הפונקציה מקבלת את שם הפעולה ואת מצב הקלט, Begin כשהמקש נלחץ. השרת אחראי לכללי המשחק, לכן הלקוח לא מבצע דאש בעצמו: הוא מבקש זאת מהשרת דרך ה-RemoteEvent Remotes.Ability, לפי שם. השרת מחזיק טבלה של יכולות לפי שם, ודאש הוא דחיפה: הגדרת המהירות של השורש מטיחה את הדמות קדימה.
function abilities.Dash(player, character, root)
root.AssemblyLinearVelocity = root.CFrame.LookVector * Settings.DashSpeed + Vector3.new(0, 15, 0)
endאתגר
בינוניב-BattleClient: קבל את ContextActionService ואת תיקיית Remotes, כתוב את use(name, state) כך שהיא תפעיל את remotes.Ability עם השם במצב Begin, וקשר את "Dash" למקש Q עם לחצן מגע.
ב-BattleServer: הוסף את local remotes = game.ReplicatedStorage.Remotes, טבלת abilities עם Dash, וחבר את remotes.Ability.OnServerEvent(player, name) כדי להפעיל את היכולת ששמה צוין עם השחקן, הדמות שלו וה-root שלה.
לחץ על Play ולחץ על Q. לאחר מכן לחץ על Check.
נסו בעצמכם
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
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)
end
end
end
-- כל שחקן מקבל Kills בלוח המובילים, וחרב בכל פעם שהוא חוזר לחיים
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Parent = leaderstats
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)
השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.