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

How do you increase a stat on a leaderboard?

I have a leader board set up and it works fine - I can see it when I test the game, yet I cannot find out how I would increase the stat for jumps made. Ex: I want the leader board to display how many jumps a player has made.

This is the current script: *note the commented part was just to make it easier to copy the code into different places

local function  onPlayerJoin(player)	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local jumps = Instance.new("IntValue")
	jumps.Name = "Jumps"
	jumps.Value	= 0
	jumps.Parent = leaderstats
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	level.Value	= 0
	level.Parent = leaderstats
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

--local character = script.Parent
--	local humanoid = character:WaitForChild("Humanoid")
--	local JumpCounter = 0
--	JumpCounter = jumps
--	humanoid.Jumping:Connect(function()
--		JumpCounter = JumpCounter + 1
--		end)

My question is how would I get it to count the jumps? Nothing I have tried so for has worked. Do I need to make a new script somewhere else? Or is keeping it in the PlayerSetup ok?

3 comments

0

uh just add the Jump value

0

also you can simply just listen for the event inside the onPlayerJoin function

0

The "Jumping" event fires twice: once when the player enters that state and once when they leave that state. I'd recommend being careful of that when you implement a system like this.

2 answers

✓ Accepted answer

To achieve this, you just need to get the Character from the player object, which you have already obtained from the PlayerAdded event.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local jumps = Instance.new("IntValue")
    jumps.Name = "Jumps"
    jumps.Value = 0
    jumps.Parent = leaderstats

    local level = Instance.new("IntValue")
    level.Name = "Level"
    level.Value = 0
    level.Parent = leaderstats

	local DebounceTime = .5
	local CanJump = true
    local char = player.Character or player.CharacterAdded:Wait()
    local humanoid = char:WaitForChild("Humanoid")
	humanoid:GetPropertyChangedSignal("Jump"):Connect(function()
		if humanoid.Jump == true and CanJump then
			jumps.Value = jumps.Value + 1
			CanJump = false
			wait(DebounceTime)
			CanJump = true
		end
	end)
end)

##Accept and upvote if this helps!

12 comments

0

This works, kind of. It doesn't seem to detect every jump made, and I can't seem to figure out when it decides to count a jump.

0

before the if statement but still within the statechanged event, print(old,new) and see what you get

0

When it doesnt count a jump it displays "Enum.Humanoid.StateType.Freefall" and displays "EnumHumanoidStateType.Landed" twice. When it does count a jump it displays "Enum.Humanoid.StateType.Jumping" then displays "Enum.Humanoid.StateType.Freefall" twice, and Enum.HumanoidStateType.Landed" twice

0

okay i think i figured out a solution. check the edit

0

It worked, thanks so much. Btw as I never used this site before, how do you mark a post answered?

0

Oh there is one minor problem -- I have a reset stat button that works properly, but with the working jump detection if I jump after resting stats, it starts counting from the last amount. Ex: if the jumps made is 6 and I reset it to 0, and jump again, it sets the jumps made to 7

0

your issue is probably that you're resetting the stats locally. show me your code here https://codeshare.io/5Oo9pP

0

accept answer button should be somewhere around here http://prntscr.com/mi4b06

0

I pasted my code in

0

check it again

0

Alright, it worked, thanks. Btw I dont see an accept answer button :c

0

its fine. ill get a mod to do it

Inside of the Humanoid there is "Jump" so using the Changed function this can be done Accept answer if this helped``

local function  onPlayerJoin(player)    
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local jumps = Instance.new("IntValue")
    jumps.Name = "Jumps"
    jumps.Value = 0
    jumps.Parent = leaderstats

    local level = Instance.new("IntValue")
    level.Name = "Level"
    level.Value = 0
    level.Parent = leaderstats
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local JumpCounter = 0
JumpCounter = jumps

humanoid.Jump.Changed:Connect(function()
if humanoid.Jump == true then
JumpCounter = JumpCounter + 1
elseif humanoid.Jump == false then
print("ended")
end
end)

2 comments

2

you cant call .Changed on a property

2

^to go along with that comment, it would be better to use humanoid:GetPropertyChangedSignal("Jump"):Connect(function()

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