How do I update a local script based on the leaderstats?

I have a starterPlayerScript that gives me “jumpies” that are basically extra air jumps. I want the player to earn extra double jumps as some sort of currency, so i made a leaderstats with the quantity. When I start the game, everything works fine, the player starts with 3 air jumps. But when I give him more jumps, the amount of jumps stays at three. At first, i was giving them to the client so it didnt work, but then i made onjects in the workspace give them so i thought it should work now. i figured it might not be checking constantly how many jumps the player has so I tried to make a while true loop that just made max jumps = jumpies then waits a bit before looping but i didnt work. i tried everything short of just restarting the game. I tried to use remote functions because i thought maybe the local script couldnt access the server to see how many jumpies i have in the leaderstats and return it but i wasnt sucessful. The codes are a whole mess now and im desperate.

This is the code that gives me the ability to double jump. its in the starterPlayerScripts and doesnt contain the amount of double jumps i have, it links to your leaderstat to see that

--double jump script

local UserInputService = game:GetService("UserInputService")
local localPlayer = game.Players.LocalPlayer
local character
local humanoid 
local canDoubleJump = false
local hasDoubleJumped = false
local gate = false
local leaderstats = game:GetService("Players").LocalPlayer:WaitForChild("leaderstats")
local jumpies = leaderstats:WaitForChild("Jumpies").Value

--
local maxJumps = jumpies
local jumpsRemaining = maxJumps
local jumpMultiplier = 2
--

local oldPower
--Add to stop them from doing both inmediately
local TIME_BETWEEN_JUMPS = 0.15
--The force of the second jump


	
	
function onJumpRequest()

	if not character or not humanoid or not character:IsDescendantOf(workspace) or
		humanoid:GetState() == Enum.HumanoidStateType.Dead then
		return
	end
	
	

	if canDoubleJump and jumpsRemaining > 0 and gate == false then
		gate = true
		hasDoubleJumped = true
		jumpsRemaining -= 1
		humanoid.JumpPower = oldPower * jumpMultiplier
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		task.wait(.2)
		gate = false

	end

end

local function characterAdded(newCharacter)
	character = newCharacter
	humanoid = newCharacter:WaitForChild("Humanoid")
	hasDoubleJumped = false
	canDoubleJump = false
	oldPower = humanoid.JumpPower

	humanoid.StateChanged:connect(function(old, new)
		if new == Enum.HumanoidStateType.Landed then
			canDoubleJump = false
			hasDoubleJumped = false
			jumpsRemaining = maxJumps
			humanoid.JumpPower = oldPower
		elseif new == Enum.HumanoidStateType.Freefall then
			wait(TIME_BETWEEN_JUMPS)
			canDoubleJump = true
		end
	end)
end

if localPlayer.Character then
	characterAdded(localPlayer.Character)
end


localPlayer.CharacterAdded:connect(characterAdded)
UserInputService.JumpRequest:connect(onJumpRequest)




--Debug

--[[while true do
	print(hasDoubleJumped)
	print (jumpsRemaining)
	wait (1)
end]]


This is the code for the leaderstat

local Players = game:GetService("Players")

game.Players.PlayerAdded : Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local jumpies = Instance.new("IntValue")
	jumpies.Name = "Jumpies"
	jumpies.Value = 3
	jumpies.Parent = leaderstats	

end)

This is the code for “coins” that give you more jumps

local Players = game:GetService("Players")

local goldChunk = script.Parent

local function onPartTouch(otherPart)
	local partParent = otherPart.Parent
	local player = Players:GetPlayerFromCharacter(partParent)
	local leaderstats = player and player:FindFirstChild("leaderstats")
	local goldStat = leaderstats and leaderstats:FindFirstChild("Jumpies")

	if goldStat then
		-- Destroy the pickup
		goldChunk:Destroy()

		-- Update the player's leaderboard stat
		goldStat.Value = goldStat.Value + 10
		 
	end
end

goldChunk.Touched:Connect(onPartTouch)

Thanks guys! Also I didnt come up with most of the double jump script, I copied it some years ago and couldnt find who it was to give them credit, so sorry!

1 Like

You only set the value of jumpies initially at the very start of the script. What you can do instead is make jumpies the object that holds the count, then adjust the value of MaxJumps whenever the object changes value.

i.e.

--double jump script

local UserInputService = game:GetService("UserInputService")
local localPlayer = game.Players.LocalPlayer
local character
local humanoid 
local canDoubleJump = false
local hasDoubleJumped = false
local gate = false
local leaderstats = game:GetService("Players").LocalPlayer:WaitForChild("leaderstats")
local jumpies = leaderstats:WaitForChild("Jumpies")

--
local maxJumps = jumpies
local jumpsRemaining = maxJumps
local jumpMultiplier = 2
--

jumpies.Changed:Connect(function()
    maxJumps = jumpies.Value
end)

local oldPower
--Add to stop them from doing both inmediately
local TIME_BETWEEN_JUMPS = 0.15
--The force of the second jump


	
	
function onJumpRequest()

	if not character or not humanoid or not character:IsDescendantOf(workspace) or
		humanoid:GetState() == Enum.HumanoidStateType.Dead then
		return
	end

	if canDoubleJump and jumpsRemaining > 0 and gate == false then
		gate = true
		hasDoubleJumped = true
		jumpsRemaining -= 1
		humanoid.JumpPower = oldPower * jumpMultiplier
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		task.wait(.2)
		gate = false
	end

end

local function characterAdded(newCharacter)
	character = newCharacter
	humanoid = newCharacter:WaitForChild("Humanoid")
	hasDoubleJumped = false
	canDoubleJump = false
	oldPower = humanoid.JumpPower

	humanoid.StateChanged:connect(function(old, new)
		if new == Enum.HumanoidStateType.Landed then
			canDoubleJump = false
			hasDoubleJumped = false
			jumpsRemaining = maxJumps
			humanoid.JumpPower = oldPower
		elseif new == Enum.HumanoidStateType.Freefall then
			wait(TIME_BETWEEN_JUMPS)
			canDoubleJump = true
		end
	end)
end

if localPlayer.Character then
	characterAdded(localPlayer.Character)
end


localPlayer.CharacterAdded:connect(characterAdded)
UserInputService.JumpRequest:connect(onJumpRequest)




--Debug

--[[while true do
	print(hasDoubleJumped)
	print (jumpsRemaining)
	wait (1)
end]]
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.