Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you increase a stat on a leaderboard?

Asked by 5 years ago
Edited 5 years ago

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?

0
uh just add the Jump value User#23365 30 — 5y
0
also you can simply just listen for the event inside the onPlayerJoin function User#23365 30 — 5y
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. User#25115 0 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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!

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. spyderwolf 15 — 5y
0
before the if statement but still within the statechanged event, print(old,new) and see what you get Gey4Jesus69 2705 — 5y
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 spyderwolf 15 — 5y
0
okay i think i figured out a solution. check the edit Gey4Jesus69 2705 — 5y
View all comments (8 more)
0
It worked, thanks so much. Btw as I never used this site before, how do you mark a post answered? spyderwolf 15 — 5y
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 spyderwolf 15 — 5y
0
your issue is probably that you're resetting the stats locally. show me your code here https://codeshare.io/5Oo9pP Gey4Jesus69 2705 — 5y
0
accept answer button should be somewhere around here http://prntscr.com/mi4b06 Gey4Jesus69 2705 — 5y
0
I pasted my code in spyderwolf 15 — 5y
0
check it again Gey4Jesus69 2705 — 5y
0
Alright, it worked, thanks. Btw I dont see an accept answer button :c spyderwolf 15 — 5y
0
its fine. ill get a mod to do it Gey4Jesus69 2705 — 5y
Ad
Log in to vote
-2
Answered by
danglt 185
5 years ago

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
you cant call .Changed on a property Gey4Jesus69 2705 — 5y
2
^to go along with that comment, it would be better to use humanoid:GetPropertyChangedSignal("Jump"):Connect(function() Vulkarin 581 — 5y

Answer this question