Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make auto-saving script that works (Not same guy below)?

Asked by 2 years ago
Edited 2 years ago

This script also adds 1 point per second. It's in sss. No errors.

Not saving somehow.

local dataStore = game:GetService("DataStoreService"):GetDataStore("randommeusumtime")

starterRolls = 0


game.Players.PlayerAdded:Connect(function(plr)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local points = Instance.new("IntValue")
    points.Name = "Time Spent" 
    points.Value = dataStore
    points.Parent = leaderstats
    while true do
        wait(1)
        points.Value = points.Value + 1
    end

end)

game.Players.PlayerRemoving:Connect(function(plr)

    dataStore:SetAsync(plr.UserId, plr.leaderstats.points.Value)

end)

3 answers

Log in to vote
3
Answered by
appxritixn 2235 Moderation Voter Community Moderator
2 years ago

Use game:BindToClose. When the server closes, there is a good chance that game.Players.PlayerRemoving will not fire, which would be why your script is not saving.

As OfficerBrah pointed out, you need to set the variable's value to the value from the datastore, not to the datastore itself:

points.Value = dataStore:GetAsync(plr.UserId)
0
Even after all your help, the script still wont save!! I dont get it. but good try on attempting to fix it. Jakob_Cashy 79 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

The name of the IntValue is "Time Spent," but you are referencing it as "points."

dataStore:SetAsync(plr.UserId, plr.leaderstats["Time Spent"].Value)

Since there is a space in the instance name, you have to use [] to path it.

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

^^^ yeah, you used the "points" variable to reference the IntValue you created inside the function, but the Name property is what the rest of your game sees

also on line 14, you set the value of the "Time Spent" IntValue to the datastore itself, rather than the data that is stored in the datastore. Replace it with this:

points.Value = dataStore:GetAsync(plr.UserId)

and I think it should work

Answer this question