Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make an add win to leaderboard script?

How do I make an add win to leaderboard script?

So I have a leadeboard script with a number value inside it so it saves:

game.Players.PlayerAdded:connect(function(player)
local DataStore = game:GetService("DataStoreService")

	 local leader = Instance.new("Folder",player)
leader.Name = "leaderstats"

for i,v in pairs(script:GetChildren()) do 
local d = DataStore:GetDataStore(v.Name)
local x = Instance.new("NumberValue",leader)
x.Name = v.Name
x.Value = d:GetAsync(player.UserId) or v.Value



end
end)

game.Players.PlayerRemoving:Connect(function(player)
	for i,v in pairs(script:GetChildren()) do 
		print("Getting")
		local DataStore = game:GetService("DataStoreService")
		local d = DataStore:GetDataStore(v.Name)
		d:SetAsync(player.UserId, player.leaderstats[v.Name].Value)
		print("Saved")
	end
end)

I also have an on win function:

return function(winner)
	
    
end
	
	
end)

My question is, what script do I need inside the win function to give a win to the player? Thanks!

4 comments

0

Is it an ontouch or an onclick?

0

no.

0

I took your script and tested it. The problem was that I couldn't even see the leaderstats in the game. Unless they're supposed to be invisible.

0

It's because its a saving leaderboard which requires a value

2 answers

Simple! You only have to use this line:

player:WaitForChild("leaderstats").FindFirstChildOfClass("NumberValue").Value += 1

Make sure you make a variable named "player"

Links: https://developer.roblox.com/en-us/api-reference/function/Instance/FindFirstChildOfClass and https://developer.roblox.com/en-us/api-reference/function/Instance/WaitForChild

How it works: the script will wait for the folder "leaderstats" to load, then, using FindFirstChildOfClass, it will make the number value have 1 more point, win, etc

1 comment

0

Thanks for the giving an answer! It isnt working for some reason, btw the leaderboard is in serverscriptservice.

Hello! Okay, so I got a solution. This has worked in my games so, I hope it works for you!

return function(winner)

        local Player = game.Players.LocalPlayer
	local Stats = Player:WaitForChild("leaderstats")
	local Money = Stats:WaitForChild("Wins") -- Or whatever the value is named
	       
	      Money.Value = Money.Value + 5

end)

There may be an error since I didn't get to test the return function. Hope this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.