Menu
CoddyTech

Nadir Evcil Hayvan Haberleri

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

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.

challenge icon

Görev

Zor

Nadirlik düzeyi Efsanevi olan evcil hayvanları herkese duyur.

  1. PetServer içinde local pet = givePet(...) satırını koru; Rarity değeri Legendary ise remotes.Announce:FireAllClients(player.Name, name) satırını kullan.
  2. PetClient içinde remotes.Announce.OnClientEvent, (who, pet) değerlerini alır: hud.Banner.Text değerini who .. " hatched a " .. pet .. "!" olarak ayarla, banner'ı göster, 4 saniye bekle ve gizle.

“Oyna” düğmesine bas ve yumurtadan bir evcil hayvan çıkar: 50 yumurtada 1 çıkan bir Ejderha, her ekranda sarı bir banner görüntüler. Ardından “Kontrol et” düğmesine bas.

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 durduğ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,
-- 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 oyuncunun liderlik tablosunda Coins değeri olur
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 öğesinin, sağlığı
-- 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 ve oyuncunun evcil hayvanlarının gücü kadar eksiltir;
-- 0'a indiğinde 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üvenmeyin: herkes
-- bu remote'u herhangi bir yerden, istediği kadar hızlı ve istediği verilerle 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), çekilişteki 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 coin 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ı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 oyun dünyasında bir parçası vardır;
-- 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
				-- geri kaldırıldı: parçası da kaldırılır
				model:Destroy()
				petModels[pet] = nil
			end
		end
	end
end)

-- İstemci, bir evcil hayvanı dışarı çıkarmayı veya geri kaldırmayı ister. Sunucu şunları kontrol eder: evcil hayvan
-- oyuncunun kendisine 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)

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

Kendi başına pratik yap: Roblox playground