Datastore not saving Player's Walk Speed

For additional confirmation I tested your script and got the following:

You’re probably not getting this error because you’re pressing “Stop” in the test menu, which means PlayerRemoving is inconsistent as the script doesn’t have enough time to fully run before the play session is shut down.

But even in the real game your speed doesn’t save when you leave after buying speed upgrade. Here Try: Clicking MONSTERS! 👹 [V1.03] - Roblox It saves everything but your speed.

Well yes, my theory is that the error message will persist in a live game as well as it can’t actually find the Humanoid. The only reason you’re not getting the error message is because I assume PlayerRemoving doesn’t have enough time to fully fire.

Is everything else in the character, or is it in the player?

Everything else is in the Player.
In the leaderstats Folder.

This would most likely line up with my theory then, give this a try and let me know if it changes.

Also I would highly recommend not using the following code for an actual live game, if you plan on converting this to live game code try to add a pcall for the :GetAsync and try to reduce the potential memory leaks that come along with this.

local DSS = game:GetService("DataStoreService")
local PlayerSave = DSS:GetDataStore("Walk")

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		
		local WalkSpeed = PlayerSave:GetAsync("WalkSpeed-"..plr.UserId)
		if WalkSpeed == nil then
			WalkSpeed = 16
		end
		local hum = char:WaitForChild("Humanoid")
		local NewValue = Instance.new("IntValue")
		NewValue.Name = "Walkspeed"
		NewValue.Value = WalkSpeed
		hum.WalkSpeed = WalkSpeed
		NewValue.Parent = plr
		hum:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
			NewValue.Value = hum.WalkSpeed
		end)
	end)
end)	



game.Players.PlayerRemoving:Connect(function(plr)
	PlayerSave:SetAsync("WalkSpeed-"..plr.UserId,plr.Walkspeed.Value)
end)

It doesn’t work I tried adding it into my script but it did nothing.
Is there a certain thing I have to do?

1 Like

Ight, lets do this :
[Similar to @alphadoggy111 's solution, but with a pcall]

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

game.Players.PlayerAdded:Connect(function(Player)
	local speedData = DataStore:GetAsync("WalkSpeed-"..Player.UserId)
	local SpeedValue = Instance.new("IntValue",Player)
	SpeedValue.Name = "PlayerSpeed"
	
	
	
	local success,error = pcall(function()
		SpeedValue.Value = speedData
	end)
	if error then
		SpeedValue.Value = 16
	end
	
	Player.CharacterAdded:Connect(function(Char)
		local hum = Char:WaitForChild("Humanoid")
		if hum then
			hum.WalkSpeed = SpeedValue.Value
			SpeedValue.Changed:Connect(function()
				hum.WalkSpeed = SpeedValue.Value
			end)
		end
		
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local success,error = pcall(function()
		DataStore:SetAsync("WalkSpeed-"..Player.UserId,Player.PlayerSpeed.Value)
	end)
	if not success then
		warn(error)
	else
		print(success)
	end
end)

image

Do I have to save the speed variable in my Datastore?

Please notice to my edit, it is important.

The change was, we want to load the data outside of the CharacterAdded event.

Yes, and you then do some checks, to see when hes back, if he has data or not, and load their speed accordingly.

Notice to more changes I did, now it works 100%

I can just put my whole script here and you can fix the Walk Speed thing.

Final script :

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

game.Players.PlayerAdded:Connect(function(Player)
	local speedData = DataStore:GetAsync("WalkSpeed-"..Player.UserId)
	local SpeedValue = Instance.new("IntValue",Player)
	SpeedValue.Name = "PlayerSpeed"
	
	
	
	local success,error = pcall(function()
		SpeedValue.Value = speedData
	end)
	if error then
		SpeedValue.Value = 16
	end
	
	Player.CharacterAdded:Connect(function(Char)
		local hum = Char:WaitForChild("Humanoid")
		if hum then
			hum.WalkSpeed = SpeedValue.Value
			SpeedValue.Changed:Connect(function()
				hum.WalkSpeed = SpeedValue.Value
			end)
		end
		
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local success,error = pcall(function()
		DataStore:SetAsync("WalkSpeed-"..Player.UserId,Player.PlayerSpeed.Value)
	end)
	if not success then
		warn(error)
	else
		print(success)
	end
end)
1 Like
local DataStoreService = game:GetService("DataStoreService")
local PlayerSave = DataStoreService:GetDataStore("PlayerSave")
local RemoteEvents = game.ReplicatedStorage.RemoteEvents
local SpeedMultiplier = RemoteEvents.SpeedMultiplier

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		task.wait()
		local PlayerSpeed = Character.Humanoid.WalkSpeed
		local leaderstats = Player.leaderstats
		local Clicks = leaderstats.Clicks
		local BaseStats = Player.BaseStats
		local Speed = BaseStats.Speed
		local ClicksData,WalkSpeed,Restarts,ButtonPresses
		local success, errormessage = pcall(function()
			ClicksData = PlayerSave:GetAsync("Clicks-"..Player.UserId)
			Restarts = PlayerSave:GetAsync("Restarts-"..Player.UserId)
			ButtonPresses = PlayerSave:GetAsync("ButtonPresses-"..Player.UserId)
			WalkSpeed = PlayerSave:GetAsync("WalkSpeed-"..Player.UserId)
		end)
		if success then
			if ClicksData then
				Player.leaderstats.Clicks.Value = ClicksData
			else
		end
			if success then
				warn(errormessage)
				if WalkSpeed == nil then
					Player.Character.Humanoid.WalkSpeed = 16
					print("This is DataStore WalkSpeed "..WalkSpeed)
				else
					Player.Character.Humanoid.WalkSpeed = WalkSpeed
					local NewValue = Instance.new("IntValue")
					NewValue.Name = "Walkspeed"
					NewValue.Value = WalkSpeed
					Character.Humanoid.WalkSpeed = WalkSpeed
					NewValue.Parent = Player
					Character.Humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
						NewValue.Value = Character.Humanoid.WalkSpeed
					end)
				end
				if success then
					if ButtonPresses then
						Player.BaseStats.ButtonPresses = ButtonPresses
					end
					if success then
						if Restarts then
							Player.leaderstats.Restarts.Value = Restarts
						else
							Player.leaderstats.Restarts.Value = 1
						end
					end
				end
			end
		end
	end)
	
	game.Players.PlayerRemoving:Connect(function(Player)
		local success, errormessage = pcall(function()
			game:BindToClose(function()
			PlayerSave:SetAsync("Clicks-"..Player.UserId,Player.leaderstats.Clicks.Value)
			PlayerSave:SetAsync("Restarts-"..Player.UserId,Player.leaderstats.Restarts.Value)
			warn("Binded to close. I saved everything!")
			PlayerSave:SetAsync("WalkSpeed-"..Player.UserId,Player.Character.Humanoid.WalkSpeed)
			PlayerSave:SetAsync("ButtonPresses-"..Player.UserId,Player.BaseStats.ButtonPresses.Value)
			end)
		end)
	end)
end)

What happens here, is we set the WalkSpeed accordingly to our value’s value. And when that value is changed, we change the walkspeed accordingly

Instead of using too many calls, you just should have a table, and save all your values that inside of that table.

Can you show me an example with my script?

How do you save Tables to datastore?, hopefully it can help you.

I apologize man, I need to go, but you can use the principle of this article and my example to help you.

Good luck!

Yeah, I recommend having a single datastore named something like “MainData” and save it as a table with the real data inside like WalkSpeed and ClicksData.