LEADERBOARD DENGAN COIN

 LEADERBOARD + COIN


1. Buatlah part coin terlebih dahulu


2. Kemudian tambahkan script di dalam part coin. Copas script dibawah ini! Besar kecil tulisan akan berpengaruh bug.

local DataStoreService = game:GetService("DataStoreService")
local CoinsDataStore = DataStoreService:GetDataStore("CoinsDataStore")

local RespawnTime = 5 -- Change the number to adjust how often you want the coin to respawn

function savePlayerData(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local coins = leaderstats:FindFirstChild("Coins")
if coins then
CoinsDataStore:SetAsync(player.UserId .. "_Coins", coins.Value)
end
end
end

function loadPlayerData(player)
local success, result = pcall(function()
return CoinsDataStore:GetAsync(player.UserId .. "_Coins")
end)
if success and result ~= nil then
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
local coins = leaderstats:FindFirstChild("Coins")
if coins then
coins.Value = result
end
end
end
end

function onTouch(otherPart)
local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then

return
end
local coins = leaderstats:FindFirstChild("Coins")
if coins then
coins.Value = coins.Value + 1 -- Change the +1 to how much currency you want that coin to give
end
script.Parent.CanTouch = false 
script.Parent.Transparency = 1
wait(RespawnTime)
script.Parent.CanTouch = true
script.Parent.Transparency = 0

end
end

function onPlayerRemoving(player)
savePlayerData(player)
end

function onPlayerAdded(player)
loadPlayerData(player)
end

script.Parent.Touched:Connect(onTouch)
game.Players.PlayerRemoving:Connect(onPlayerRemoving)
game.Players.PlayerAdded:Connect(onPlayerAdded)

3. Kemudian cari ServerScriptService dan tambahkan script (rename menjadi playersetupcoin)
untuk varibel sebelumnya bernama coin, maka sesuaikan juga. Jika namanya berbeda maka akan terjadi bug. Copas script dibawah ini!
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local Coin = Instance.new("IntValue")
Coin.Name = "Coins" --  Change this to what you want the leaderstat name to be
Coin.Value   = 0
Coin.Parent = leaderstats
end)  

4. Lakukan Playtest

Komentar

Postingan populer dari blog ini

CSS GRID PADA HTML

Projek3