Player's data (in this case the number of laps completed on a race track) keeps resetting after a few days/rejoining several times. However, the script kind of works since it saves the data for a short period of time. Please help me improve the script, as i have been trying for a long time and cant figure it out.
First script is getting the laps after driving through the 2 checkpoints and the finish line:
function Entered(player)
wait()
if player:findFirstChild("leaderstats") ~= nil then
player.leaderstats:remove()
end
local stats = Instance.new("IntValue")
stats.Parent = player
stats.Name = "leaderstats"
local laps = Instance.new("IntValue")
laps.Parent = stats
laps.Name = "Laps"
laps.Value = 0
local first = Instance.new("IntValue")
first.Name = "Cheeze"
first.Parent = player
first.Value = false
end
game.Players.PlayerAdded:connect(Entered)
c = game.Players:GetChildren()
for i=1, #c do
Entered(c[i])
end
The next script is saving the data in StarterCharacterScripts:
Save=nil;
Load=nil;
function Loadup()
wait();
Save=game.Workspace.Remotes:WaitForChild("Save")
Load=game.Workspace.Remotes:WaitForChild("Load")
if Save~=nil and Load~=nil then
Load:FireServer();
Save:FireServer();
else
Loadup()
end
end
Loadup();
The next script is to manage the saves in ServerScriptService:
load=game.Workspace.Remotes.Load;
save=game.Workspace.Remotes.Save;
DataService=game:GetService("DataStoreService");
DataService:GetDataStore("Races")
DataStore=DataService:GetDataStore("MyDataStore");
function SaveValue(player)
local Success, Error=pcall(function()
DataStore:SetAsync(game.Players[player.Name].UserId.."-Laps", game.Players[player.Name].leaderstats.Laps.Value)
end)
if Success then
print("Saved Data!")
else
print("Error Saving: "..Error);
end
end
function LoadValue(player)
local Success, Error=pcall(function()
local num=DataStore:GetAsync(game.Players[player.Name].UserId.."-Laps");
if num==nil or num==0 then
SaveValue(player);
else
game.Players[player.Name].leaderstats.Laps.Value=num;
end
end)
if Success then
print("Loaded Data!")
else
print("Error Loading: "..Error);
end
end
game.Players.PlayerRemoving:Connect(function(player)
SaveValue(player)
end)
save.OnServerEvent:Connect(function(User)
SaveValue(User);
end)
load.OnServerEvent:Connect(function(User)
LoadValue(User);
end)

1 comment
I've edited your question to format it correctly. When writing a post, there's a circular "Lua" icon on the far right that puts in ~~~s for you to put your code in. There's also an Edit button below your post if you make a mistake.