Datastore not saving

  1. I want the walkspeed to save when a character is resetted or rejoin.

  2. It won’t save.

Below you can find my script.

 
local Walkspeed = DSS:GetDataStore("Walkspeed")
local i = nil
game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local success, errormsg = pcall(function()
			 i = Walkspeed:GetAsync(Player.UserId)
	end)
		if success then
			if i == 0 then Character.Humanoid.WalkSpeed = 1 else
		Character.Humanoid.WalkSpeed = i end
	else
		print("Error")
		warn(errormsg);
	end
		game.Players.PlayerRemoving:Connect(function(Player)
			local walkspeed = tonumber(Character.Humanoid.WalkSpeed)
			Walkspeed:SetAsync(Player.UserId, walkspeed)
		end)
	end)
end)```
local WalkspeedDS = DSS:GetDataStore("Walkspeed")

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local data = WalkspeedDS:GetAsync(Player.UserId)
        Character.Humanoid.WalkSpeed = data and data == 0 and 1 or data or Character.Humanoid.WalkSpeed
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local walkspeedToSave = Character:FindFirstChild("Humanoid") and Character.Humanoid.WalkSpeed or 16 -- in case you are dead when you leave, which without this or statement would error
	WalkspeedDS:SetAsync(Player.UserId, walkSpeedToSave)
end)
1 Like

Where do you make the variable for datastore service?

You are not setting walk speed inside of the characterAdded function, so it is only being applied when the player joins, probably before the character has even spawned. Set the walk speed every time the character spawns.

This would probably fix it, if you haven’t saved data it us usually nil not 0

is totally correct you’re code was misplaced probably

Please avoid putting [URGENT] in the title. We will help you when we have the chance. You need to put the success and error in the player added function. Vice versa with removing

3 Likes