Menu
CoddyTech

בקיעת ביצה

שיעור 9 מתוך 20 בקורס משחק Roblox: סימולטור חיות מחמד של Coddy.

לביצה הלבנה ליד נקודת ההופעה יש ProximityPrompt: התקרב אליה ו-Roblox יציג "Hatch" עם מקש שצריך להחזיק (E, או הקשה בטלפון). כשהשחקן מחזיק אותו מספיק זמן, ההנחיה מפעילה את Triggered בשרת, עם השחקן:

workspace.Egg.ProximityPrompt.Triggered:Connect(function(player)
end)

השרת בודק את המחיר, לוקח את המטבעות ונותן חיית מחמד. חיית מחמד היא StringValue שמכיל את שמה, בתוך תיקיית Pets שבשחקן. כל מה שהשרת מכניס לשחקן משוכפל ללקוח של אותו שחקן, וכך רשימת חיות המחמד בשיעור מאוחר יותר יודעת מה להציג.

challenge icon

אתגר

בינוני

בקוע Cat בביצה תמורת 25 מטבעות.

  1. בתוך PlayerAdded, הוסף לשחקן Folder בשם Pets.
  2. כתוב את givePet(player, name): ‏StringValue בשם Pet שהערך שלו הוא name בתוך Pets; החזר אותו.
  3. ב־workspace.Egg.ProximityPrompt.Triggered: אם Coins הוא לפחות Settings.EggPrice, הפחת אותו, קרא ל־givePet(player, "Cat") והדפס player.Name .. " hatched a Cat".

לחץ על "שחק", הרוויח 25 מטבעות והחזק את E ליד הביצה: Coins יפחת ב־25. לאחר מכן לחץ על "בדיקה".

נסו בעצמכם

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

local remotes = game.ReplicatedStorage.Remotes
local piles = workspace.Piles
local pileSpot = {} -- כל ערימה -> המקום שבו היא עומדת
local lastHit = {} -- כל שחקן -> מתי הוא פגע בפעם האחרונה

print("A Meadow pile gives " .. Settings.PileValue .. " coins")

-- כל שחקן מקבל Coins בטבלת המובילים
Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
end)

-- ערימת מטבעות במקום מסוים: עותק של ServerStorage.CoinPile עם הבריאות שלה
-- והערך שלה כמאפיינים
local function spawnPile(spot)
	local pile = ServerStorage.CoinPile:Clone()
	pile.Position = spot.Position
	pile:SetAttribute("MaxHealth", Settings.PileHealth)
	pile:SetAttribute("Health", Settings.PileHealth)
	pile:SetAttribute("Value", Settings.PileValue)
	pile.HealthGui.Label.Text = pile:GetAttribute("Health") .. "/" .. pile:GetAttribute("MaxHealth")
	pileSpot[pile] = spot
	pile.Parent = piles
	return pile
end

-- פגיעה מפחיתה 1 מהבריאות של ערימה;
-- כשהבריאות מגיעה ל-0, הערימה נשברת ומעניקה את הערך שלה
local function hitPile(player, pile)
	local health = pile:GetAttribute("Health") - 1
	if health > 0 then
		pile:SetAttribute("Health", health)
		pile.HealthGui.Label.Text = health .. "/" .. pile:GetAttribute("MaxHealth")
		return
	end
	player.leaderstats.Coins.Value += pile:GetAttribute("Value")
	local spot = pileSpot[pile]
	pileSpot[pile] = nil
	pile:Destroy()
	-- ערימה חדשה באותו מקום, כעבור כמה שניות
	task.delay(Settings.RespawnTime, spawnPile, spot)
end

-- ערימה בכל מקום ב-Meadow
for _, spot in workspace.Zones.Meadow.Spawns:GetChildren() do
	spawnPile(spot)
end

-- הלקוח מבקש לפגוע בערימה. לעולם אל תסמכו על הלקוח: כל אחד יכול להפעיל
-- את האירוע המרוחק הזה עם כל דבר, מכל מקום, ובתדירות שירצה.
remotes.Hit.OnServerEvent:Connect(function(player, pile)
	-- ערימה אמיתית
	if typeof(pile) ~= "Instance" or pile.Parent ~= piles then
		return
	end
	-- בטווח פגיעה
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if not root or (root.Position - pile.Position).Magnitude > Settings.Reach then
		return
	end
	-- לא מהר מדי
	local now = time()
	if lastHit[player] and now - lastHit[player] < Settings.HitCooldown then
		return
	end
	lastHit[player] = now
	hitPile(player, pile)
end)

-- שיעור 9: בקיעת ביצה יופיע כאן

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

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