Menu
CoddyTech

Katılırken Yükle

Coddy'nin Roblox Oyunu: Evcil Hayvan Simülatörü kursunda ders 20 / 20.

Şimdi oyuncu katıldığında kaydedilen verileri geri oku. GetAsync, SetAsync tarafından anahtar altında kaydedilen değeri döndürür veya hiç kayıt yapmamış bir oyuncu için nil döndürür. Başarısız da olabilir; bu yüzden onu, çalışıp çalışmadığını ve ardından değeri döndüren bir pcall içinde çağır:

local ok, saved = pcall(function()
	return saveStore:GetAsync(player.UserId)
end)
if ok and saved then
	print(saved.Coins)
end

Klasörleri oluşturduktan sonra yükle; böylece evcil hayvanların girebileceği bir Pets klasörü olur. petInfo içinde bulunmayan kayıtlı adları atla: oyundan kaldırılmış bir evcil hayvan, oyuncunun katılmasını engellememeli.

challenge icon

Görev

Zor

Oyunculara kaydettiklerini geri verin.

  1. PlayerAdded içinde, Pets klasörünü oluşturduktan sonra: local ok, saved = pcall(function() return saveStore:GetAsync(player.UserId) end).
  2. ok and saved doğruysa: Coins değerini saved.Coins olarak ve Desert özniteliğini saved.Desert olarak ayarlayın.
  3. petInfo içindeki kaydedilmiş her evcil hayvan için givePet(player, pet.Name, pet.Equipped == true).

“Oyna” düğmesine basın, yumurtadan evcil hayvan çıkarın, Durdur'a basın, ardından tekrar “Oyna” düğmesine basın: coin'leriniz ve evcil hayvanlarınız geri gelir. Ardından “Kontrol et” düğmesine basın.

Kendin dene

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

local remotes = game.ReplicatedStorage.Remotes
local piles = workspace.Piles
local petParts = workspace.PetParts
local saveStore = DataStoreService:GetDataStore("Pets")
local pileSpot = {} -- her yığın -> bulunduğu nokta
local lastHit = {} -- her oyuncu -> en son ne zaman vurduğu

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

-- İsme göre Settings.Pets
local petInfo = {}
for _, info in Settings.Pets do
	petInfo[info.Name] = info
end

-- Bir evcil hayvan: oyuncunun Pets klasöründe adını tutan,
-- nadirliği ve gücü öznitelik olarak saklanan bir StringValue
local function givePet(player, name, equipped)
	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:SetAttribute("Equipped", equipped)
	pet.Parent = player.Pets
	return pet
end

-- bir oyuncunun dışarıda kaç evcil hayvanı olduğu
local function equippedCount(player)
	local count = 0
	for _, pet in player.Pets:GetChildren() do
		if pet:GetAttribute("Equipped") then
			count += 1
		end
	end
	return count
end

-- bir oyuncunun evcil hayvanlarının her vuruşa kattığı güç
local function petPower(player)
	local power = 0
	for _, pet in player.Pets:GetChildren() do
		if pet:GetAttribute("Equipped") then
			power += pet:GetAttribute("Power")
		end
	end
	return power
end

-- Her oyuncuya liderlik tablosunda Coins verilir
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

	-- sahip oldukları evcil hayvanlar
	local pets = Instance.new("Folder")
	pets.Name = "Pets"
	pets.Parent = player
	-- Ders 20: Katılınca Yükleme buraya gelecek
end)

-- Bir noktadaki para yığını: ServerStorage.CoinPile nesnesinin, canı
-- ve değeri öznitelik olarak saklanan bir kopyası
local function spawnPile(spot)
	local desert = spot.Parent.Parent.Name == "Desert"
	local health = desert and Settings.DesertHealth or Settings.PileHealth
	local pile = ServerStorage.CoinPile:Clone()
	pile.Position = spot.Position
	pile:SetAttribute("MaxHealth", health)
	pile:SetAttribute("Health", health)
	pile:SetAttribute("Value", desert and Settings.DesertValue or Settings.PileValue)
	pile:SetAttribute("Zone", spot.Parent.Parent.Name)
	pile.HealthGui.Label.Text = pile:GetAttribute("Health") .. "/" .. pile:GetAttribute("MaxHealth")
	pileSpot[pile] = spot
	pile.Parent = piles
	return pile
end

-- Bir vuruş, yığının sağlığından 1 artı oyuncunun evcil hayvanlarının gücü kadar düşer;
-- sağlık 0 olduğunda yığın parçalanır ve değeri kadar ödeme yapar
local function hitPile(player, pile)
	local health = pile:GetAttribute("Health") - (1 + petPower(player))
	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()
	-- birkaç saniye sonra aynı noktada yeni bir yığın
	task.delay(Settings.RespawnTime, spawnPile, spot)
end

-- Her bölgedeki her noktada bir yığın
for _, zone in workspace.Zones:GetChildren() do
	for _, spot in zone.Spawns:GetChildren() do
		spawnPile(spot)
	end
end

-- İstemci bir yığına vurmak ister. İstemciye asla güvenme: herkes
-- bu uzaktan olayı herhangi bir yerden, herhangi bir şeyle, istediği sıklıkta tetikleyebilir.
remotes.Hit.OnServerEvent:Connect(function(player, pile)
	-- gerçek bir yığın
	if typeof(pile) ~= "Instance" or pile.Parent ~= piles then
		return
	end
	-- erişim mesafesinde
	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
	-- çok sık değil
	local now = time()
	if lastHit[player] and now - lastHit[player] < Settings.HitCooldown then
		return
	end
	lastHit[player] = now
	-- oyuncunun açtığı bir bölgede
	if pile:GetAttribute("Zone") == "Desert" and not player:GetAttribute("Desert") then
		return
	end
	hitPile(player, pile)
end)

-- Ağırlıklı bir seçim: her evcil hayvanın Chance değeri (100 üzerinden), seçim aralığındaki payıdır
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

-- Yumurtadan çıkarma: yumurtanın etkileşim istemi, bedeli jetonlarla ödenir
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()
	-- Yer varsa hemen dışarı çıkar
	local pet = givePet(player, name, equippedCount(player) < Settings.MaxEquipped)
	print(player.Name .. " hatched a " .. name)
	-- en nadir evcil hayvanlardan herkes haberdar olur
	if pet:GetAttribute("Rarity") == "Legendary" then
		remotes.Announce:FireAllClients(player.Name, name)
	end
end)

-- Dışarıdaki evcil hayvanlar sahiplerini takip eder: her birinin oyun dünyasında bir parçası vardır ve
-- her karede oyuncunun arkasında bir sıra hâlinde tutulur
local petModels = {} -- her evcil hayvan -> oyun dünyasındaki parçası
RunService.Heartbeat:Connect(function()
	for _, player in Players:GetPlayers() do
		local root = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
		local pets = player:FindFirstChild("Pets")
		local slot = 0
		for _, pet in pets and pets:GetChildren() or {} do
			local model = petModels[pet]
			if pet:GetAttribute("Equipped") and root then
				slot += 1
				if not model then
					model = ServerStorage.PetModels[pet.Value]:Clone()
					model.Parent = petParts
					petModels[pet] = model
				end
				model.CFrame = root.CFrame * CFrame.new((slot - 2) * 3, -1.5, 4)
			elseif model then
				-- içeri alındı: parçası kaldırılır
				model:Destroy()
				petModels[pet] = nil
			end
		end
	end
end)

-- İstemci bir evcil hayvanı dışarı çıkarmayı veya içeri almayı ister. Sunucu evcil hayvanın
-- oyuncuya ait olduğunu ve dışarıda MaxEquipped sayısından fazla evcil hayvan bulunmadığını kontrol eder.
remotes.Equip.OnServerEvent:Connect(function(player, pet)
	if typeof(pet) ~= "Instance" or pet.Parent ~= player.Pets then
		return
	end
	if pet:GetAttribute("Equipped") then
		pet:SetAttribute("Equipped", false)
	elseif equippedCount(player) < Settings.MaxEquipped then
		pet:SetAttribute("Equipped", true)
	end
end)

-- Desert kapısı: bir kez ödeme yapılınca yalnızca o oyuncu için açılır
workspace.Gate.ProximityPrompt.Triggered:Connect(function(player)
	local coins = player.leaderstats.Coins
	if player:GetAttribute("Desert") or coins.Value < Settings.GatePrice then
		return
	end
	coins.Value -= Settings.GatePrice
	player:SetAttribute("Desert", true)
end)

-- Kaydetme: Coins, Desert ve tüm evcil hayvanlar
local function save(player)
	local pets = {}
	for _, pet in player.Pets:GetChildren() do
		table.insert(pets, { Name = pet.Value, Equipped = pet:GetAttribute("Equipped") == true })
	end
	local data = {
		Coins = player.leaderstats.Coins.Value,
		Desert = player:GetAttribute("Desert") == true,
		Pets = pets,
	}
	pcall(function()
		saveStore:SetAsync(player.UserId, data)
	end)
end

Players.PlayerRemoving:Connect(save)

game:BindToClose(function()
	for _, player in Players:GetPlayers() do
		save(player)
	end
end)
quiz iconKendini test et

Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.

Roblox Oyunu: Evcil Hayvan Simülatörü bölümündeki tüm dersler

Kendi başına pratik yap: Roblox playground