בקש מהשרת
שיעור 6 מתוך 25 בקורס משחק Roblox: זירות קרב של Coddy.
היכולות מתחילות במכשיר של השחקן. BattleClient הוא LocalScript בתוך ה-HUD. ContextActionService מקשר פעולה למקשים, וכשמשתמשים ב-true כארגומנט השלישי, הוא גם מציג עבורה כפתור במסך הטלפון:
ContextActionService:BindAction("Wave", use, true, Enum.KeyCode.T)הפונקציה מקבלת את שם הפעולה ואת מצב הקלט — Begin כשהמקש נלחץ. השרת אחראי לכללים, לכן הלקוח רק שולח בקשה דרך ה-RemoteEvent Remotes.Ability. בצד השרת, OnServerEvent מקשיב לאירוע, ו-Roblox מעביר קודם את השחקן שהפעיל אותו:
remotes.Ability.OnServerEvent:Connect(function(player, name)
print(name .. " from " .. player.Name)
end)אתגר
בינוניהגדר את Q כך שיבקש מהשרת לבצע זינוק.
- ב־
BattleClient, כתובuse(name, state): במצבEnum.UserInputState.Begin, קרא ל־remotes.Ability:FireServer(name). - קשור את הפעולה:
ContextActionService:BindAction("Dash", use, true, Enum.KeyCode.Q). - ב־
BattleServer, חבר אתremotes.Ability.OnServerEventאלprint(player.Name .. " asked for " .. name).
לחץ על "שחק" ואז על Q: בחלון הפלט מופיע Player1 asked for Dash. לאחר מכן לחץ על "בדיקה".
נסו בעצמכם
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)
print(humanoid.Parent.Name .. " is at " .. humanoid.Health)
end
end
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 אונליין