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

Datastore loading but not saving?

Asked by 4 years ago
Edited by User#5423 4 years ago

Below is my datastore script for when the system saves two variables, 'Points' and 'Wins'. I have previously defined 'dss' as the DataStoreService, 'mds' and 'wds' as DataStores previously created (I know these variables are fine)

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


    local data

    local success, errormessage = pcall(function() --LOADING POINTS
        data = mds:GetAsync(tostring(player.UserId.."-points"))
    end)
    if success then
        print("success loading data")
        player:WaitForChild("leaderstats").Points.Value=data
        local check = tostring(data)
        print(check)
    else
        print("Error getting data")
        warn(errormessage)
    end
end)

game.Players.PlayerAdded:Connect(function(player)
    local winsdata
    local success, errormessage = pcall(function() --LOADING WINS
        winsdata = wds:GetAsync(tostring(player.UserId.."-wins"))
    end)

    if success then
        print("success loading data")
        player:WaitForChild("leaderstats").Wins.Value=winsdata
        local winscheck = tostring(winsdata)
        print(winscheck)
    else
        print("Error getting data")
        warn(errormessage)
    end
end)

These work absolutely fine and load in the variables previously saved just fine. However today when I logged onto studio, my save script suddenly wasn't working.

game.Players.PlayerRemoving:Connect(function(player)
    print("this script has run")
    local success, errormessage = pcall(function()
        print("this pcall also ran")
        mds:SetAsync(player.UserId.."-points",player.leaderstats.Points.Value)
    end)

    if success then
        print("Data saved succesfully.")
    else
        print("Error saving data")
        warn(errormessage)
        end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local asuccess, aerrormessage = pcall(function()
        print("this script has run")
        wds:SetAsync(player.UserId.."-wins",player.leaderstats.Wins.Value)
    end)

    if asuccess then
        print("Data saved succesfully.")
    else
        print("Error saving data")
        warn(aerrormessage)
        end
end)

In the output, "this script has run" appears twice as it should and so does "this pcall also ran", but there are no "Data saved successfully" or any errormessages at all. Is there a problem with the pcall function returning success? Surely even if it returned nil then the else part would still run? Please help , thanks!

0
edit:- fixed codeblock User#5423 17 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

The problem is, you are using two player removing functions and two playeradded functions and it is probably overwriting one another.

Datastores are best used when storing data in tables, this makes scripts smaller, easier to reference and more flexability. This also prevents too many datastore saving requests, which is a datastore error when too much data is being saved at one time:

https://developer.roblox.com/en-us/articles/Datastore-Errors

You should narrow this down to one DataStore Variable.

game.Players.PlayerRemoving:Connect(function(player)
    local data = { --Store the data
    points = player.leaderstats.Points.Value,
        wins = player.leaderstats.Wins.Value
        }
    if data then
        local success, err = pcall(function()
            wds:SetAsync(player.UserId.."-Stats", data) -- Set the data
        end)
        if success then
            print("Success Saving")
        end
    end
end)
game.Players.PlayerAdded:Connect(function(player)
    local Data;
    local drawn, err = pcall(function()
        data = wds:GetAsync(player.UserId.."-Stats")

    end)
    if drawn and data then
        local points = data.points
        local wins = data.wins
        player.leaderstats.Wins.Value = wins
        player.leaderstats.Points,Value = points

        else --If there was an error grabbing data and/or its a players first visit and theres no           data to retreive then 
        print("First Visit")
    end
end)

Make sure the playerAdded function is above the playerRemoving. Hope this helps, keeping your datastore organised is quintessential when it comes to clarity when you start storing bigger things like custom characters for example.

Side Note: For studio testing turn on studio access api enabled in the games settings.

0
Thank you for this - I also foudn that at times my original script worked in Studio, and in-game it definitely works. Maybe a Studio bug? Primrose_Studio 53 — 4y
0
Yeah studio can be buggy with api services evilninja77 20 — 4y
Ad

Answer this question