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

How do you add value to a leader board stat?

I know how to create a leaderboard stat, here is how I have mine setup.

game.Players.PlayerAdded:connect(function(player) --setting leaderboard up to input values stats = Instance.new("IntValue", player) stats.Name = "leaderstats" --Wins Value wins = Instance.new("IntValue", stats) wins.Name = "Wins" --Gold Value gold = Instance.new("IntValue", stats) gold.Name = "Gold" end)

This works as I have tested it, how to you increase the value when you do something, like touch a block. Any answers would be greatly appreciated, thanks!

3 answers

local part = script.Parent -- If you put this in a part of course
local inc = 5 -- By how much you want it to increase

Debounce = true -- Makes sure you don't keep touching it every 0.00000001 seconds
local DebounceTime = 1 -- Change this to the cooldown time in seconds

part.Touched:connect(function(hit) -- when the part is touched
	if hit.Parent:FindFirstChild('Humanoid') and Debounce then -- CHeck if it is a player (and the debounce is true)
		Debounce = false -- Sets the debounce to false
		local char = hit.Parent -- Gets the char
		local plr = game.Players:GetPlayerFromCharacter(char) -- Gets the player from the char

		local leaderstats = plr:WaitForChild('leaderstats') -- Gets the leaderstats
		local Gold = plr:WaitForChild('Gold') -- Change Gold to any value you want to change

		Gold.Value = Gold.Value + inc -- Adds to the value with an increment
		wait(DebounceTime) -- Wait the cooldown
		Debounce = true -- Enables the script again
	end
end)

I added comments, I hope I didn't make any writing mistakes, if you don't understand a part, please ask.

Okay, you need a leaderboard. No problem. Start with this:

game.Players.PlayerAdded:connect(function(player)
	local stats = Instance.new("IntValue", player)
	stats.Name = "leaderstats"
	wins = Instance.new("IntValue", stats)
	wins.Name = "Wins"
	gold = Instance.new("IntValue", stats)
	gold.Name = "Gold"
end)

From here, let's say that a player touches a brick to gain wins and/or gold. Add this to your current script to accomplish so:

part = script.Parent
bool = true
function onTouch()
	if bool == false then return end -- Debounce ensures that the player only gets 1 win/gold
	bool = false
	if part:FindFirstChild("Humanoid") ~= nil then
		wins.Value = wins.Value + 1 -- Or use gold.Value
	end
	wait(3)
	bool = true
end
part.Touched:connect(onTouch)

Extend from there as you wish.

game.Players.LocalPlayer["values stats"].Value + 1

not sure if this would work but it should be something like this

2 comments

0

I doubt that will work because one, you would never use something like that, 2 you should always change stats server side, not client!

0

you are exactly right xD

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