Evcil Hayvanlarını Kurtar
Coddy'nin Roblox Oyunu: Evcil Hayvan Simülatörü kursunda ders 18 / 18.
Oyuncu bir sonraki katılışında coin'ler, Desert ve tüm pet'ler hâlâ orada olmalı. Bir DataStore, her oyuncu için UserId altında bir tablo tutar. Pet'ler birer örnektir; DataStore bunları saklayamaz. Bu yüzden her pet'in adını ve dışarıda olup olmadığını içeren düz bir tablo kaydedin ve oyuncu katıldığında pet'leri bu tablodan yeniden oluşturun:
{ Coins = 120, Desert = true, Pets = { { Name = "Fox", Equipped = true } } }Oyuncu ayrıldığında (PlayerRemoving) ve sunucu kapandığında (game:BindToClose) kaydedin; PlayerAdded içinde yükleyin. Her iki çağrı da Roblox tarafında başarısız olabilir, bu yüzden bunları pcall içine alın. Roblox Studio'da DataStore'lar, oyun yayımlandığında ve API Hizmetlerine Studio Erişimini Etkinleştir seçeneği açık olduğunda çalışır.
Görev
ZorDataStoreService'i vePetsadlı bir DataStore alın.save(player)yazın: her evcil hayvan için{ Name, Equipped }listesini,Coins'i veDesert'i oyuncunun UserId'si altında bir pcall içinde kaydedin.PlayerRemovingolayında vegame:BindToCloseiçinde her oyuncu için çağırın.PlayerAddediçinde, klasörleri oluşturduktan sonra kaydı yükleyin (bir pcall içinde) ve coin'leri, Desert niteliğini ve evcil hayvanları geri yükleyin (petInfoiçinde olmayan adları atlayın).
Play'e basın, yumurtadan evcil hayvan çıkarın, Stop'a ve tekrar Play'e basın: evcil hayvanlarınız geri gelir. Ardından Check'e basın.
Kendin dene
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local RunService = game:GetService("RunService")
local Settings = require(game.ReplicatedStorage.Settings)
local remotes = game.ReplicatedStorage.Remotes
local piles = workspace.Piles
local petParts = workspace.PetParts
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")
-- ada 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 bir StringValue;
-- nadirliği ve gücü de öznitelik olarak saklanır
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 eklediği 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 oyuncu liderlik tablosunda Coins kazanır
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
end)
-- Bir noktadaki para yığını: ServerStorage.CoinPile kopyası; canı
-- ve değeri öznitelik olarak saklanır
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 canından 1 artı oyuncunun evcil hayvanlarının gücü kadar düşürür;
-- 0'a düştüğünde 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 remote'u herhangi bir yerden, herhangi bir veriyle, istediği hızda 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 hızlı 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 çekiliş: Her pet'in Chance değeri (100 üzerinden), aralıktaki 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 prompt'u, ücreti coins ile ö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 bu parçalar
-- 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
model:Destroy()
petModels[pet] = nil
end
end
end
end)
-- İstemci bir evcil hayvanı dışarı çıkarmayı veya geri almayı ister. Sunucu evcil hayvanın
-- oyuncuya ait olduğunu ve dışarıda en fazla MaxEquipped evcil hayvan bulunduğunu 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ıldığında 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)
Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.