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

Why doesnt my datastore save my datas?

Asked by 4 years ago

So, I made this script I learned from a youtuber. and it worked!! and then I tried adding another data. it failed :(. and it wont work. can someone please help me find my issue? I've been struggling for much to long XD

--DataStore Script  
    local player = NewPlayer
    local playerUserId = "Player "..player.UserId

    local datar
    local data
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(playerUserId)
        datar = myDataStore:GetAsync(playerUserId)
    end)

    if success then
        Coins.Value = data
        RunSpeed.Value = datar
        print("Data Loaded")
    end
end)
game.Players.PlayerRemoving:Connect(function(player)

    local playerUserId = "Player "..player.UserId

    local data =  player.leaderstats.Pennys.Value
    local datar = player.leaderstats.RunSpeed.Value

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId, data)
        myDataStoreRun:SetAsync(playerUserId, datar)
    end)

    if success then
        print("Data Successfully Saved!")
        print("RunSpeed Successfully Saved!")
    else
        print("Something Went Wrong :(")
        warn(errormessage)
    end
end)

And for some reason, theres no errors! it doesnt say "Something Went Wrong :(" it doesnt say "Data Successfully Saved!" it doesnt have any output It will load but theres nothing to load, cause you cant save

1 answer

Log in to vote
1
Answered by
iOwn_You 543 Moderation Voter
4 years ago
Edited 4 years ago

When the last player in the server leaves PlayerRemoving doesnt fire. To save the data of the last player in the server you should use game:BindToClose() This will run the function before the server shuts down and save the data of the last player.

Keep in mind; BindToClose() will only keep the server from shutting down until the function finishes running or until 30 seconds have passed from the moment it started so make sure your function doesn't take that long to execute or you will have dataloss.

BindToClose API

EDIT: I just noticed another problem with your code, you overwrite the data youre trying to save, you should store all the data you want to save in 1 array and save that array to the datastore:

Arrays tutorial

0
But where would i put the :BindToClose? Galaxybombboy 134 — 4y
0
Anywhere in your script outside of PlayerRemoving, usually i would put it below it. Its an event so it would fire when it happens, no matter where you place it. iOwn_You 543 — 4y
Ad

Answer this question