Nadir Evcil Hayvan Haberleri
Coddy'nin Roblox Oyunu: Evcil Hayvan Simülatörü kursunda ders 17 / 18.
Birisi Efsanevi bir evcil hayvan çıkardığında, tüm sunucu bunu bilmelidir. FireAllClients, her oyuncunun istemcisinde aynı anda bir RemoteEvent tetikler:
-- sunucu
remotes.Announce:FireAllClients(player.Name, name)
-- istemci
remotes.Announce.OnClientEvent:Connect(function(who, pet)
end)Her istemci, haberi birkaç saniyeliğine HUD'daki Banner üzerinde gösterir. OnClientEvent, sunucunun gönderdiği şeyi olduğu gibi alır: bu tarafta oyuncu eklenmez.
Görev
ZorPetServeriçinde, yumurtadan çıkma sonrasında petin Rarity değeriLegendaryise, oyuncunun Name değerini ve petin adını kullanarakremotes.Announceile tüm istemcilere bildirim gönderin.PetClientiçinde,remotes.Announce.OnClientEventüzerinde Banner'ın metniniPlayer1 hatched a Dragon!olarak ayarlayın, banner'ı gösterin, 4 saniye bekleyin ve gizleyin.
Play'e basın, ardından Check'e basın: kontroller bir Dragon'ın yumurtadan çıkmasını sağlar.
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 -> üzerinde 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 ve
-- nadirliği ile gücünü öznitelik olarak saklayan 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
-- 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 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 jeton yığını: ServerStorage.CoinPile'ın, canını
-- ve değerini öznitelik olarak saklayan 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 canından 1 artı oyuncunun evcil hayvanlarının gücü kadar eksiltir;
-- 0'a ulaştığında yığın parçalanır ve değeri kadar coin kazandırır
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üvenmeyin: Herkes
-- bu remote'u istediği yerden, istediği verilerle ve 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 seçim: Her pet'in Chance değeri (100 üzerinden), toplam içindeki 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 coin ile ödeme yapılan etkileşim istemi
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ıda
local pet = givePet(player, name, equippedCount(player) < Settings.MaxEquipped)
print(player.Name .. " hatched a " .. name)
end)
-- Dışarıdaki evcil hayvanlar sahiplerini takip eder: her birinin dünyada bir parçası vardır; bunlar
-- her karede oyuncunun arkasında bir sıra hâlinde tutulur
local petModels = {} -- her evcil hayvan -> dünyadaki 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ı çıkarmak veya geri almak ister. Sunucu şunu kontrol eder: evcil hayvan
-- oyuncuya aittir ve dışarıdaki evcil hayvan sayısı MaxEquipped değerini aşmaz.
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)
Bu ders kısa bir quiz içerir. Soruları yanıtlamak ve ilerlemeni kaydetmek için derse başla.