Menu
CoddyTech

חיית מחמד עוקבת אחריכם

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

חיות המחמד אמורות לעקוב אחריך. חיית מחמד חדשה יוצאת מיד אם פחות מ־Settings.MaxEquipped (3) נמצאות בחוץ, ומסומנת באמצעות מאפיין Equipped.

כל חיית מחמד שנמצאת בחוץ מקבלת חלק בעולם, עותק של המודל שלה מתוך ServerStorage.PetModels, ובכל פריים השרת מציב אותה מאחורי הבעלים שלה. RunService.Heartbeat מופעל פעם אחת בכל פריים, וכפל של CFrames מציב אחד ביחס לאחר:

model.CFrame = root.CFrame * CFrame.new(0, -1.5, 4) -- 4 סטאדים מאחור, קצת נמוך יותר

טבלה זוכרת איזה חלק שייך לאיזו חיית מחמד, כך שחיית מחמד שמכניסים בחזרה מאבדת את החלק שלה.

challenge icon

אתגר

בינוני
  1. כתבו את equippedCount(player). הוסיפו ל־givePet ארגומנט שלישי, equipped, ששמור בתור המאפיין Equipped, ובקעו ביצה עם equippedCount(player) < Settings.MaxEquipped.
  2. קבלו את RunService ואת local petParts = workspace.PetParts. בכל פעימת Heartbeat, עבור כל שחקן ועבור כל חיית מחמד: אם היא בחוץ (ולשחקן יש דמות), ודאו שיש לה חלק (עותק של ServerStorage.PetModels[pet.Value] בתוך petParts) והציבו אותו ב־root.CFrame * CFrame.new((slot - 2) * 3, -1.5, 4), כשסופרים את המקומות 1, 2, 3; אם היא הוחזרה, השמידו את החלק שלה.

לחצו על Play, בקעו ביצה וצאו להליכה. לאחר מכן לחצו על Check.

נסו בעצמכם

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")

-- Settings.Pets לפי שם
local petInfo = {}
for _, info in Settings.Pets do
	petInfo[info.Name] = info
end

-- חיית מחמד: StringValue בתיקיית Pets של השחקן, שמכיל את שמה, עם
-- הנדירות והכוח שלה כמאפיינים
local function givePet(player, name)
	local info = petInfo[name]
	local pet = Instance.new("StringValue")
	pet.Name = "Pet"
	pet.Value = name
	pet:SetAttribute("Rarity", info.Rarity)
	pet:SetAttribute("Power", info.Power)
	pet.Parent = player.Pets
	return pet
end

-- כל שחקן מקבל 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

	-- חיות המחמד שבבעלותם
	local pets = Instance.new("Folder")
	pets.Name = "Pets"
	pets.Parent = player
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)

-- הגרלה משוקללת: ה-Chance של כל חיית מחמד (מתוך 100) הוא חלקה בטווח
local function rollPet()
	local roll = math.random() * 100
	for _, info in Settings.Pets do
		roll -= info.Chance
		if roll <= 0 then
			return info.Name
		end
	end
	return Settings.Pets[1].Name
end

-- בקיעה: התשלום על ההנחיה של הביצה מתבצע במטבעות
workspace.Egg.ProximityPrompt.Triggered:Connect(function(player)
	local coins = player.leaderstats.Coins
	if coins.Value < Settings.EggPrice then
		return
	end
	coins.Value -= Settings.EggPrice
	local name = rollPet()
	givePet(player, name)
	print(player.Name .. " hatched a " .. name)
end)
quiz iconבחנו את עצמכם

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

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