Saving player walk speed and jump power stats with datastore

– Code

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

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

local leaderstats = Instance.new(“Folder”, plr)
leaderstats.Name = “” --[[ Change this to the name of your leaderboard name it leaderboard if you want to see the leaderboard, if not set it to a random name, that’s you can remember --]]

local speed = Instance.new(“IntValue”, plr)
speed.Name = “speed”

local jump = Instance.new(“IntValue”, leaderstats)
jump.Name = “jump”

local level = Instance.new(“IntValue”, leaderstats)
level.Name = “level”

local data = DataStoreService:GetAsync(plr.UserId)

if data then
– We know the player has data

speed.Value = data.speed

Jump.Value = data.jump

level.Value = data.level

else

print(“First time playing”)

– Set to default values
speed.Value = 16
jump.Value = 30
level.Value = 0

end

player.CharacterAdded:Connect(function(char)

task.spawn(function()

while wait() do

– Setting walkspeed and jumpower

speed.Value = char:WaitForChild(“Humanoid”).WalkSpeed
jump.Value = Humanoid.JumpPower

end

end)

end)

end)

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

local TableGettingSaved = {}

for i, v in pairs(plr:WaitForChild(“leaderstats”):GetChildren()) do

table.insert(TableGettingSaved, {Name = v.Name, Value = v.Value})

end

DSS:SetAsync(plr.UserId, TableGettingSaved)

end) – I updated the code, try this code instead!

1 Like

Embeded Version:
local DSS = game:GetService(“DataStoreService”):GetDataStore(“SpeedDSS”)

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

local leaderstats = Instance.new(“Folder”, plr)
leaderstats.Name = “” --[[ Change this to the name of your leaderboard name it leaderboard if you want to see the leaderboard, if not set it to a random name, that’s you can remember --]]

local speed = Instance.new(“IntValue”, plr)
speed.Name = “speed”

local jump = Instance.new(“IntValue”, leaderstats)
jump.Name = “jump”

local level = Instance.new(“IntValue”, leaderstats)
level.Name = “level”

local data = DataStoreService:GetAsync(plr.UserId)

if data then
	– We know the player has data

	speed.Value = data.speed

	Jump.Value = data.jump

	level.Value = data.level

else

	print(“First time playing”)

	– Set to default values
	speed.Value = 16
	jump.Value = 30
	level.Value = 0

end

player.CharacterAdded:Connect(function(char)

	task.spawn(function()

		while wait() do

			– Setting walkspeed and jumpower

			speed.Value = char:WaitForChild(“Humanoid”).WalkSpeed
			jump.Value = Humanoid.JumpPower

		end

	end)

end)

end)

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

local TableGettingSaved = {}

for i, v in pairs(plr:WaitForChild(“leaderstats”):GetChildren()) do

	table.insert(TableGettingSaved, {Name = v.Name, Value = v.Value})

end

DSS:SetAsync(plr.UserId, TableGettingSaved)

end)

1 Like

You do have data in datastore, but you already saved zeroes.
Fix it by using ternary operator

Humanoid.WalkSpeed = if speed.Value > 0 then speed.Value else 20 end
Humanoid.JumpPower = if jump.Value > 0 then jump.Value else 30 end

I have already told the user to change the datastore name as a way to reset data and replace it with updated values in my other updated code, but im sure that could also be another approach rather than having to change the datastore name multiple times.

1 Like

Changing data store name is bad practice and shouldn’t be used. This way of testing would create lot of unused data. Changing the name should be used only when there’s a plan for global wipe.
Managing data store is difficult tho, I’ve been forced to create my own migration lib to ensure that data will always have correct structure even after game updates.

A lot of variables in your script were inconsistent or undefined, plus the quotes were in a format incompatible with mine so I had to fix a bunch of stuff and may have missed a few things.

Yeah I don’t really know what else to do, and just for context, here is the current script I have.

local DSS = game:GetService("DataStoreService"):GetDataStore("SpeedDSS")

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

	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "SpeedStore" --[[ Change this to the name of your leaderboard name it leaderboard if you want to see the leaderboard, if not set it to a random name, that’s you can remember --]]

	local speed = Instance.new("IntValue", leaderstats)
	speed.Name = "speed"

	local jump = Instance.new("IntValue", leaderstats)
	jump.Name = "jump"

	local level = Instance.new("IntValue", leaderstats)
	level.Name = "idk"

	local data = DSS:GetAsync(plr.UserId)

	if data then
		-- We know the player has data

		speed.Value = data.speed

		jump.Value = data.jump

		level.Value = data.level

	else

		print("First Time Playing")

		-- Set to default values
		level.Value = 0

	end

	plr.CharacterAdded:Connect(function(char)

		task.spawn(function()

			while wait() do

				-- Setting walkspeed and jumpower

				speed.Value = char:WaitForChild("Humanoid").WalkSpeed
				jump.Value = char:WaitForChild("Humanoid").WalkSpeed

			end

		end)

	end)

end)

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

	local TableGettingSaved = {}

	for i, v in pairs(plr:WaitForChild("SpeedStore"):GetChildren()) do

		table.insert(TableGettingSaved, {Name = v.Name, Value = v.Value})

	end

	DSS:SetAsync(plr.UserId, TableGettingSaved)

end) 

I honestly kind of just give up, unless someone can send a model of this working then there is no way I am going to get this to work.

You still have bug in your code which I fixed before…

speed.Value = char:WaitForChild("Humanoid").WalkSpeed
jump.Value = char:WaitForChild("Humanoid").WalkSpeed

Do you understand what is happening there? You’re loading your played data, use Value objects to store the loaded data and then you rewrite them by humanoid values…
Just reverse it… You gotta understand what you’re doing, if you want it working.

You’ve probably got confused from Tom’s answers, where he brings the bug back again.
You’ve already got the solution in my answers, so use that.

I understand that I need to reverse it, and even though Tom’s help is much appreciated, it still threw me off a bit but I do not understand your provided solution.

The script is underlining it saying to close the character added function above with a parenthesis even though one is already there.

Full script again if it helps

local DSS = game:GetService("DataStoreService"):GetDataStore("SpeedDSS")

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

	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "SpeedStore" --[[ Change this to the name of your leaderboard name it leaderboard if you want to see the leaderboard, if not set it to a random name, that’s you can remember --]]

	local speed = Instance.new("IntValue", leaderstats)
	speed.Name = "speed"

	local jump = Instance.new("IntValue", leaderstats)
	jump.Name = "jump"

	local level = Instance.new("IntValue", leaderstats)
	level.Name = "idk"

	local data = DSS:GetAsync(plr.UserId)

	if data then
		-- We know the player has data

		speed.Value = data.speed

		jump.Value = data.jump

		level.Value = data.level

	else

		print("First Time Playing")

		-- Set to default values
		level.Value = 0

	end

	plr.CharacterAdded:Connect(function(char)
		local Humanoid = char:FindFirstChild("Humanoid")
		-- Setting walkspeed and jumpower
		Humanoid.WalkSpeed = if speed.Value > 0 then speed.Value else 20 end
		Humanoid.JumpPower = if jump.Value > 0 then jump.Value else 30 end
			
		
	end)

end)



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

	local TableGettingSaved = {}

	for i, v in pairs(plr:WaitForChild("SpeedStore"):GetChildren()) do

		table.insert(TableGettingSaved, {Name = v.Name, Value = v.Value})

	end

	DSS:SetAsync(plr.UserId, TableGettingSaved)

end) 

My bad, ternary operator in lua looks different:
speed.Value > 0 and speed.Value or 20
Same goes for jump.

I’m still a bit confused, but I know there has to be an equal sign somewhere in the expression too like this I think:

local value = speed.Value > 0 and speed.Value or 20

because without any operator its invalid

Humanoid.WalkSpeed = speed.Value > 0 and speed.Value or 20
Humanoid.JumpPower = jump.Value > 0 and jump.Value or 30
1 Like

I think I just answered my own question:

Humanoid.WalkSpeed = speed.Value > 0 and speed.Value or 20
Humanoid.JumpPower = jump.Value > 0 and jump.Value or 20

yeah didn’t see that until after my previous reply but thanks anyways lol

I implemented it and nothing seemed to have changed, so I put some print statements in there and they did not print anything.

local DSS = game:GetService("DataStoreService"):GetDataStore("SpeedDSS")

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

	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "SpeedStore" --[[ Change this to the name of your leaderboard name it leaderboard if you want to see the leaderboard, if not set it to a random name, that’s you can remember --]]

	local speed = Instance.new("IntValue", leaderstats)
	speed.Name = "speed"

	local jump = Instance.new("IntValue", leaderstats)
	jump.Name = "jump"

	local level = Instance.new("IntValue", leaderstats)
	level.Name = "idk"

	local data = DSS:GetAsync(plr.UserId)

	if data then
		-- We know the player has data

		speed.Value = data.speed
		jump.Value = data.jump
		level.Value = data.level

	else

		print("First Time Playing")

		-- Set to default values
		level.Value = 0

	end

	plr.CharacterAdded:Connect(function(char)
		local Humanoid = char:FindFirstChild("Humanoid")
		-- Setting walkspeed and jumpower
		Humanoid.WalkSpeed = speed.Value > 0 and speed.Value or 20
		Humanoid.JumpPower = jump.Value > 0 and jump.Value or 20
		
		print(Humanoid.WalkSpeed .. " Walkspeed+Datastore: "..speed.Value)
		print(Humanoid.JumpPower .. " Walkspeed+Datastore: "..jump.Value)
	end)

end)



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

	local TableGettingSaved = {}

	for i, v in pairs(plr:WaitForChild("SpeedStore"):GetChildren()) do

		table.insert(TableGettingSaved, {Name = v.Name, Value = v.Value})

	end

	DSS:SetAsync(plr.UserId, TableGettingSaved)

end) 

Yeah now I think I’m a bit more stuck and confused.

If it didn’t print your humanoid stats, then it means your character has been loaded before you added consumer to that signal. Use the example I showed you Here.

– I have updated the script, this should work, if not you may have API Services disabled:

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

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

local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "SpeedStore" --[[ Change this to the name of your leaderboard name it leaderboard if you want to see the leaderboard, if not set it to a random name, that’s you can remember --]]

local speed = Instance.new("IntValue", leaderstats)
speed.Name = "speed"

local jump = Instance.new("IntValue", leaderstats)
jump.Name = "jump"

local level = Instance.new("IntValue", leaderstats)
level.Name = "level" 

local data = DSS:GetAsync(plr.UserId)

if data then

	-- We know the player has data

	speed.Value = data.speed

            jump.Value = data.jump
	level.Value = data.level

else

	print("First Time Playing")

	-- Set to default values
	level.Value = 0

end

plr.CharacterAdded:Connect(function(char)

	local Humanoid = char:FindFirstChild("Humanoid")

	-- Setting walkspeed and jumpower

	Humanoid.WalkSpeed = speed.Value > 0 and speed.Value or 20
	Humanoid.JumpPower = jump.Value > 0 and jump.Value or 20
	
	print(Humanoid.WalkSpeed .. " Walkspeed+Datastore: "..speed.Value)
	print(Humanoid.JumpPower .. " Walkspeed+Datastore: "..jump.Value)

end)

end)

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

local TableGettingSaved = {}

for i, v in pairs(plr:WaitForChild("SpeedStore"):GetChildren()) do

	TableGettingSaved[v.Name] = {Name = v.Name, Value = v.Value} 

end

DSS:SetAsync(plr.UserId, TableGettingSaved)

end) – This is the corrected version, if it is incorrect, please tell me and i will edit the code and fix the issue.

I should assume that walkspeed and jumpheight are just float values (EX: 16.4) and can be saved as such just like any other number.