בובת אימונים
שיעור 5 מתוך 20 בקורס משחק Roblox: זירות קרב של Coddy.
בובת האימון זקוקה לשני דברים כדי להיות בובת אימון: מד בריאות, ושיופיע מחדש כשמנצחים אותה.
ה־HealthBar שלה הוא BillboardGui עם Fill ירוק בתוך Back כהה. UDim2.fromScale(health / MaxHealth, 1) קובע את רוחב המילוי לפי כמות הבריאות שנותרה.
כשה־Humanoid שלה מת (Died), זכרו היכן הוא עמד (GetPivot), הסירו אותו, המתינו Settings.DummyRespawn שניות והציבו במקומו עותק חדש מ־ServerStorage. צריך להגדיר גם את הבובה החדשה באותו אופן, לכן ההגדרה היא פונקציה שקוראת לעצמה עבור הבובה החדשה:
local function setupDummy(dummy)
-- סרגל בריאות, וכאשר Died מתרחש: ... setupDummy(fresh)
end
setupDummy(workspace.Dummy)אתגר
קלבסוף הסקריפט, כתבו setupDummy(dummy):
- בעת
HealthChangedשל ה-Humanoid, התאימו את הגודל שלdummy.HealthBar.Back.Fillלבריאות שנותרה. - בעת
Died: שמרו אתdummy:GetPivot(), המתינו 0.5 שניות, השמידו אותו, המתינו ל-Settings.DummyRespawn, שכפלו אתServerStorage.Dummy, מקמו אותו שם, הגדירו את workspace כהורה שלו והגדירו אותו.
קראו לה עבור workspace.Dummy. לחצו על Play, ואז על 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)
השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.