How to save player walkspeed

Hi fellow devs,

i want my datastore script to save the player walkspeed, i have already searched on the devforum and youtube but i couldnt find anything usefull. Can someone help me? This is my datastore script

local dataStore = game:GetService("DataStoreService"):GetDataStore("TestVersion1")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrid = "id_"..plr.UserId
	local save1 = plr.leaderstats.Stage
	local save2 = plr.leaderstats.Wins

	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
end)

game.Players.PlayerRemoving:Connect(function(plr)
	dataStore:SetAsync("id_"..plr.UserId, {plr.leaderstats.Stage.Value,plr.leaderstats.Wins.Value})
	print("Saved data")
end)

Thanks already!

tell me if it helps sorry for messy code lol
local dataStore = game:GetService(“DataStoreService”):GetDataStore(“TestVersion1”)
local speedData = game:GetService(“DataStoreService”):GetDataStore(“areyouseriousrightnoew”)
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrid = “id_”…plr.UserId
local save1 = plr.leaderstats.Stage
local save2 = plr.leaderstats.Wins
local speeddata
local key = plr.UserId
pcall(function()
speeddata = speedData:GetAsync(key)
end)
if speeddata ~= nil then
plr.Character.Humanoid.WalkSpeed = speeddata
end
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
end)

game.Players.PlayerRemoving:Connect(function(plr)
dataStore:SetAsync(“id_”…plr.UserId, {plr.leaderstats.Stage.Value,plr.leaderstats.Wins.Value})
speedData:SetAsync(plr.UserId,plr.Character.Humanoid.WalkSpeed)
print(“Saved data”)
end)

2 Likes

what isn’t working? does it error, does it not save, did you enable the api to use datastore in studio?

1 Like

Hi, thanks for the reply!

It doesnt work unfortunatly, it gives this error
image

1 Like

Hi,

the problem is that i dont know how to save the players walkspeed

1 Like

using Humanoid.WalkSpeed but considering it’s saving when the player is leaving it would probably be a good idea to store their walkspeeds somewhere then request walkspeed for that player when they leave.You could use a table to store each players walkspeed and update it every time it changes, then get that value when they leave

1 Like

just make a intvalue in replicatedstorage for every player that joins and it updates every second
or whenever u want and whenever the player leaves find the players intvalue and save it in the walkspeed data thats how i usually handle with these stuff lol

1 Like

i dont really know how to do this im not really a scripter. Can you show me how to make a script like that?

1 Like

oh yeah sure
game.Players.PlayerAdded:Connect(function(plr)
local walkspeed = Instance.new(“IntValue”,plr)
walkspeed.Name = “walkspeed”
end)
that should be in serverscript
put this in the player character

game.RunService.HeartBeat:Connect(function()
local plr = game.Players:GetPlayerFromCharacter(script.Parent)
if plr then
if plr.walkspeed then
plr.walkspeed.Value = script.Parent:FindFirstChild(“Humanoid”).WalkSpeed
end
end
end)
remember the both scripts shouldnt be localscripts
and just save the walkspeed if u dont know how the tell me

2 Likes

where do i put this script? You said player character but that doesnt work

1 Like

no errors ???

1 Like

nope, i put the server script in the startercharacterscript as you told me

1 Like

wait a sec, it does give this error
image

1 Like

The one you showed me needs to be in the startercharacterscripts and the other one in serverscripts

1 Like

OOH OKKKK just do this game[“Run Service”]

1 Like

let me give u updated script (sorry if i sound like chat gpt)

game.[“Run Service”].HeartBeat:Connect(function()
local plr = game.Players:GetPlayerFromCharacter(script.Parent)
if plr then
if plr.walkspeed then
plr.walkspeed.Value = script.Parent:FindFirstChild(“Humanoid”).WalkSpeed
end
end
end)

1 Like

i got this error now :
image

1 Like

and after u load in the walkspeed apply it to the humanoid

1 Like

remove the . before ["Run Service"]

2 Likes

ok final script this should work
game[“Run Service”].HeartBeat:Connect(function()
local plr = game.Players:GetPlayerFromCharacter(script.Parent)
if plr then
if plr.walkspeed then
plr.walkspeed.Value = script.Parent:FindFirstChild(“Humanoid”).WalkSpeed
end
end
end)

1 Like