I need help with datastore

Hello, I am doing a simulator, which is called Speed Simulator. At this point, I need the game to save the humanoid walkspeed, once they leave the game, it’d be boring re-start everytime you rejoin! Is there a script to do it?

Answers are appreciated! :smiley_cat:
Have a good day/night!

1 Like

Dont save the humanoid walkspeed.

Instead add an int value inside your player which stores your speed. So when you leave it can save the data and when you join back the intvalue data will be changed to the players humanoid walkspeed

Watch this tutorial to get a basic idea of how it will be done

Tutorial

Then If you need help message me

1 Like

First if you dont know you need to turn on api services in settings.

Now first make an intvalue

game.Players.PlayerAdded:Connect(function(plr)
local fol = Instance.new("Folder")
fol.Name = "Stats"
fol.Parent = plr
local speed = Instance.new("IntValue")
speed.Name = "Speed"
speed.Parent = fol
end)

Put this inside serverstorage.

Now for the datastorage

local DTS = game:GetService("DataStoreService")

local my_data = DTS:GetOrderedDataStore("Speed") -- Name for Datastore

game.Players.PlayerAdded:Connect(function(plr)
	local speed= plr:WaitForChild("Stats"):WaitForChild("Speed")
	
	local data;
	
	local success, errormsg = pcall(function()
		data = my_data:GetAsync(plr.UserId)
	end)
	if success then
		speed.Value = data
	else
		print("Error")
		warn(errormsg);
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errormsg = pcall(function()
		my_data:SetAsync(plr.UserId, plr.leaderstats.Stats.Speed.Value)
	end)
	if success then
		print("Player Data Successfully stored.")
	else
		print("Error");
		warn(errormsg)
	end
end)

game.Players.PlayerAdded:Connect(function(plr)
	while wait(40) do
	local success, errormsg = pcall(function()
		my_data:SetAsync(plr.UserId, plr.leaderstats.Stats.Speed.Value)
	end)
	if success then
		print("Player Data Successfully stored.")
	else
		print("Error");
		warn(errormsg)
	end
	end
end)

This is will save the Intvalue

now set the intvalue to humanoids WalkSpeed.

game.Players.PlayerAdded:Connect(function(plr)
local speed = plr:WaitForChild("Stats"):WaitForChild("Speed").Value
while true do
wait()
plr.Character.Humanoid.WalkSpeed = speed
end
end)

This is a basic example, this might not work properly so you would have to optimize it.

I will explain edit the code with comments to explain what it does later as I am using mobile right now, watch the tutorial I linked to get a better idea.

Where do I place the thing that sets the value to the walkspeed?

All should go inside serverScriptService

1 Like

Sadly, it’s not working :frowning:

What is the error in the output?

error

The stats is not created yet. In fact, you should do CharacterAdded:Wait() to make sure the folder is loaded in. You can also do :WaitForChild('Stats', 100). They work the same.
Don’t give up!

CharacterAdded:Wait()
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(character)
-- The script here
end)
end)
:WaitForChild('Stats', 100)
local speed = plr:WaitForChild('Stats', 100):WaitForChild('Speed', 100)

Change the local speed line to this.

I am just going to hire a scripter to do it.

It’s very easy, you don’t need to hire a scripter just for this small warning. You can refer to my reply above. Or you can hire me haha.

1 Like

Like @RealJefff2000 said, it is just a small error in a small script. He already provided the solution above. But if you do hire a scripter, make sure it’s voluntary work since it would not be worth it to pay anyone for this heh. Ideally, you could try experimenting around with your script; that way you would learn yourself and would not need help (or less help) from others when you encounter a problem. :smiley:

1 Like