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

DataStore help. It doesn't save my stats, how do I fix this?

Hello so, I am trying to make a game on ROBLOX, pretty simple were each second you get +1 jump power, now I got the jump power thingy up and running, but I want to do so it saves when a player leaves and joins, so far I found this code on the internet, but nothing worked, here is the code.

local DataStores = game:GetService("DataStoreService") local FirstDataStore = DataStore:GetDataStore("JumpPowerStorage")

game.Players.PlayerAdded:connect(function(player) wait(0.1) player.Character.Humanoid.JumpPower = FirstDataStore:GetAsync(player.UserId) or 0 FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.JumpPower) player.Character.Humanoid.Changed:connect(function() FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.JumpPower) end) end) game.Players.PlayerRemoving:connect(function(player) FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.JumpPower) end)

please help me fix it, thanks.

2 answers

✓ Accepted answer

Inside the variable FirstDataStore, you use "DataStore", and not the actual variable "DataStores". Replace DataStore with DataStores and you should be good to go.

local DataStores = game:GetService("DataStoreService")
local FirstDataStore = DataStores:GetDataStore("JumpPowerStorage")

game.Players.PlayerAdded:connect(function(player)
	wait(0.1)
	player.Character.Humanoid.JumpPower = FirstDataStore:GetAsync(player.UserId) or 0 
	FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.JumpPower)
	player.Character.Humanoid.Changed:connect(function() 
		FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.JumpPower) 
	end)
end)

game.Players.PlayerRemoving:connect(function(player) 
	FirstDataStore:SetAsync(player.UserId, player.Character.Humanoid.JumpPower)
end)

1 comment

0

it still doesn't save the jump power after rejoining

Try this

local DataStores = game:GetService("DataStoreService")
local FirstDataStore = DataStores:GetDataStore("JumpPowerStorage")

game.Players.PlayerAdded:Connect(function(player)
	wait(0.1)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		humanoid.JumpPower = FirstDataStore:GetAsync(player.UserId) or 0
		humanoid.Changed:Connect(function()
			FirstDataStore:SetAsync(player.UserId, humanoid.JumpPower)
		end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	if player.Character then
		local humanoid = player.Character:WaitForChild("Humanoid")
		FirstDataStore:SetAsync(player.UserId, humanoid.JumpPower)
	end
end)

1 comment

0

Hey, it works now, thank you very much.

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