local ds = game:GetService("DataStoreService")
local player = game:GetService("Players")
local rep = game:GetService("ReplicatedStorage")
local statsf = rep:WaitForChild("Stats")
local datastore = ds:GetDataStore("Stats")
local function saveData(player)
local speed = statsf:WaitForChild(player.Name):WaitForChild("Speed")
local strength = statsf:WaitForChild(player.Name):WaitForChild("Strength")
local data = {
speed.Value,
strength.Value
}
local success, err = pcall(function()
datastore:SetAsync(player.UserId, data)
end)
if success then
print("Data has been saved!")
else if err then
print("Data hasn't been saved!")
warn(err)
end
end
end
player.PlayerAdded:Connect(function(plr)
local userID = plr.userId
local stats = Instance.new("Folder")
wait (.1)
stats.Parent = statsf
stats.Name = plr.Name
local Speed = Instance.new("IntValue")
Speed.Name = ("Speed")
Speed.Parent = stats
local Strength = Instance.new("IntValue")
Strength.Name = ("Strength")
Strength.Parent = stats
local data
local success, err = pcall(function()
data = data:GetAsync(player.UserId)
end)
if success then
Speed.Value = data[1]
Strength.Value = data[2]
elseif err then print ("No data found")
Speed.Value = 12
Strength.Value = 0
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved")
elseif err then
print("Data has not been saved!")
end
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Data has been saved")
elseif err then
print("Data has not been saved!")
end
end
end)
Also, if there is any way to improve upon this script, let me know! Thank you

1 comment
thanks for the quick reply! I have just swapped over to datastore2