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

Why is my datastore script not printing successes or fails?

Asked by 4 years ago

I'm trying to make my game save the cash players have and came up with this

local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("myDataStore")


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

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


    local cash = Instance.new("IntValue")
    cash.Name = "Cash"
    cash.Parent = leaderstats





    local data
    local success, errormessage = pcall(function()

         data = MyDataStore:GetAsync(player.UserId.."-cash")
    end)


    if success then

        cash.Value = data
    else
            print("error")
            warn(errormessage)
    end
end)






game.Players.PlayerRemoving:Connect(function(player)
    local success, errormessage = pcall(function()
    MyDataStore:SetAsync(player.UserId.."-cash",player.leaderstats.Cash.Value)

    end)
    if success then print("Data Saved")
        else print("error")
            warn("errormessage")

    end





end)

Roblox says there is nothing that needs to be corrected --ex. unknown value-- and nothing is printed (the errors)

1 answer

Log in to vote
1
Answered by 4 years ago

The reason it doesn't give an output comes down to the general way GetAsync works. You see, it attempts to pull users from the given datastore. If there is no data then it just won't get anything. In a normal case without a pcall function, it wouldn't error either. So having it in a pcall when there is no userdata in it renders it useless. This sets the data to nil and you cannot set an int value's value to nil only numbers.

The workaround:

data = MyDataStore:GetAsync(player.UserId.."-cash") or 0

Simple as that. If no value can be found we set it to 0.

So next time you run the script then leave and rejoin, it should work.

0
Oh and make sure StudioAccessToAPIServices are enabled! namespace25 594 — 4y
0
No but the print is on the player removing not on the player added Barty200 85 — 4y
Ad

Answer this question