Data store is not saving my JumpHeight

I am making a game where you gain +1 jump per second. The problem i am having is that the jump stat is not saving. Here is my code.

local function addJump(hum, Wins, plr, Jump)
spawn(function()
while wait(1) do
hum.JumpHeight += 1

		Wins.Value = plr.leaderstats.Wins.Value

		if Wins.Value >= 1 then
			hum.JumpHeight = hum.JumpHeight + Wins.Value
		end
	end
end)

end

local DSS = game:GetService(“DataStoreService”)
local myDSS = DSS:GetDataStore(“MyDataStore”)

local dataStore = game:GetService(“DataStoreService”):GetDataStore(“DataStore”)

game.Players.PlayerAdded:Connect(function(plr)
local jumpH

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr

local Wins = Instance.new("IntValue")
Wins.Name = "Wins"
Wins.Parent = leaderstats

local Jump = Instance.new("IntValue")
Jump.Name = "Jump"
Jump.Parent = leaderstats

local plrid = "id_"..plr.userId
local save1 = plr.leaderstats.Wins
local save2 = plr.leaderstats.Jump

local GetSaved = dataStore:GetAsync(plrid)
if GetSaved then
	save1.Value = GetSaved[1]
	save2.Value = GetSaved[2]
else
	local NumberforSaving = {save1.Value, save2.Value}
	dataStore:GetAsync(plrid, NumberforSaving)
end

plr.CharacterAdded:Connect(function(char)
	local hum = char:WaitForChild("Humanoid")

	if jumpH then
		hum.JumpHeight = jumpH
	end

	addJump(hum, Wins, plr, Jump)

	hum.Died:Connect(function()
		jumpH = hum.JumpHeight
	end)
end)

end)

game.Players.PlayerRemoving:Connect(function(plr)

dataStore:SetAsync("id_"..plr.userId, {plr.leaderstats.Wins.Value, plr.leaderstats.Jump.Value})
print("wins = ",  plr.leaderstats.Wins.Value)
print("jump = ",  plr.leaderstats.Jump.Value)

end)

game:BindToClose(function()
for i, players in pairs(game.Players:GetChildren()) do
players:Kick(“server closed”)
end
end)

This is the output
bug
The jump doesn’t save

also idk if this matters but i have my character JumpHeight set to 25
ss

1 Like

Here’s a more efficient way of saving

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")

local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerInfo")

Players.PlayerAdded:Connect(function(player)

	local UserId = player.UserId
	local Key = "Player_".. UserId -- The DataKey
	
	local InfoFolder = script.Info:Clone() -- Clones Info Folder into Player
	InfoFolder.Parent = player
	
	local Success, Ret
	
	repeat
	Success, Ret = pcall(DataStore.GetAsync, DataStore, Key)
		if Success then
			local PlayerInfo = Ret
		
			for i, v in ipairs(InfoFolder:GetChildren()) do -- Grabs the values from the folder
				if v then
					if PlayerInfo then -- A check
						if PlayerInfo[v.Name] then -- Checks if data store has a data with the same name as one of the values in the folder
							v.Value = PlayerInfo[v.Name]  -- Sets the value in that folder as the value set in the datastore
						end
					end
					
				end
			end
		end
		

		if Success then
			warn("Success")
		else
			warn("Failed")
		end
		
		wait()
	until Success

end)

Players.PlayerRemoving:Connect(function(player)

	local UserId = player.UserId
	local Key = "Player_".. UserId
	
	local Success, Ret
	
	repeat

			local PlayerInfo = {} -- Table for data to be put in and saved
				
		for i, v in ipairs(player.Info:GetChildren()) do -- Gets values from the value folder
			if v then
				PlayerInfo[v.Name] = v.Value -- Adds the name and value of the child in the folder to the table
			end
		end
		
		Success, Ret = pcall(DataStore.SetAsync, DataStore, Key, PlayerInfo) -- Saves table (Dictionary basically)
		
		if Success then
			warn("Success")
		else
			warn("Failed")
		end
		
		wait()
	until Success
	
end)

This datastore method allows you to place any IntValue or BoolValue or StringValue, etc. into the folder and it instantly becomes a variable that is saved. It’s all automatic.

Info

1 Like

Okay I put that in my game and I added IntValues now how do I connect them to the player, because right now they just equal zero. I was thinking that I add the leaderstats and plug it into the PlayerInfo on line 55… idk

So, the reasons they equal zero is because you have to set the numbers inside the folder to what you want them to spawn with.

You can either place the leaderstats right after the variable “InfoFolder” in the code above.

Then to place a value in the leaderstat folder, after data is loaded. you could move the variable to the leaderstat folder and then when they’re leaving the game move it back to the info folder.

local Players = game:GetService(“Players”)

Players.PlayerAdded:Connect(function(player)
wait(3)
local wins = player.Info.Wins
local jump = player.Info.Jump
while wait(.5) do
wins.Value = player.leaderstats.Wins.Value
jump.Value = player.leaderstats.Jump.Value
end

end)

So I added that script to the ServerScriptService and the same problem keeps happening. I keep getting 0 for the jump Value but Wins is working and returning as 7.
ss1
bug1

I know the bug has something to do with constantly changing the characters jumpheight.

Instead of loop through the values, there is another method

Jump.Changed:Connect(function(change)
jump.Value = player.leaderstats.Jump.Value
end)

wins.Changed:Connect(function(change)
wins.Value = player.leaderstats.Wins.Value
end)