I’m making that if a coin is collected you earn a point,and this script worked in my game in the past,but for some reason it’s not working anymore,i have tried changing the name of the point and leaderstats but it simply doesn’t work.
Script:
amnt = 1
function onTouched(part)
local h = part.Parent:findFirstChild(“Humanoid”)
if (h~=nil) then
local thisplr = game.Players:findFirstChild(h.Parent.Name)
if (thisplr~=nil) then
local stats = thisplr:findFirstChild(“leaderstats”)
if (stats~=nil) then
local score = stats:findFirstChild(“Coins”)
if (score~=nil) then
score.Value = score.Value + amnt
end
end
end
script.Parent:remove()
end
end
When creating posts that involve code on the DevForum, please try to format your code so it's easier to help you. I wrote the guide below which should hopefully help you with this.
To create a leaderboard and increment an int value ,
such as in your case “coins”, you would :
Create a Server Script in Server Script Service for the leaderboard :
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)--for each player , when they join
--create a leaderboard for them
local leaderstats = Instance.new("Folder")
local coins = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
coins.Name = "Coins"
leaderstats.Parent = player
coins.Parent = leaderstats
end)
Next to increase the value each time a coin is touched, an efficient method (rather than creating a separate script for each coin) would be to :
Create a folder in workspace called CoinsFolder
insert all your coins (just the parts) to the folder
position your coins wherever you want to
loop through all coins to increase the value : described below
You would now add some line below the above code (in the same script I mean) :
local CoinsFolder = workspace:WaitForChild("CoinsFolder")
amt = 10--how much each coin will give
debounce = true-- so the player doesn't get hundreds of coins in one touch
for _, coin in ipairs(CoinsFolder:GetChildren()) do
coin.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player and debounce then
debounce=false
local lead = player:WaitForChild("leaderstats")
local coins = lead.Coins
coins.Value = coins.Value + amt
wait(0.5)--how much time he has to wait before his coins value is increased again
debounce=true
end
end)
end
So in the end your complete script in Server Script Service would look like :
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
local coins = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
coins.Name = "Coins"
leaderstats.Parent = player
coins.Parent = leaderstats
end)
local CoinsFolder = workspace:WaitForChild("CoinsFolder")
amt = 10
debounce = true
for _, coin in ipairs(CoinsFolder:GetChildren()) do
coin.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player and debounce then
debounce=false
local lead = player:WaitForChild("leaderstats")
local coins = lead.Coins
coins.Value = coins.Value + amt
wait(0.5)
debounce=true
end
end)
end
But remember , to ensure that these values save, you would need to use Datastores