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

Why isn't my newer DataStore not load/saving any data?

Asked by 3 years ago
Edited 3 years ago

I recently changed up my DataStore System and was wondering why is wasn't working (I have tried this out of studio)any ideas? any help would be appreciated

local DataStoreService = game:GetService("DataStoreService")
local DATA = DataStoreService:GetDataStore("DATA")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

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

    local data
    local success, errormessage = pcall (function()
        data = DATA:GetAsync(player.UserId.."-cash")
    end)
    if success then
        cash.Value = data
    else
        print("An error occurred while loading player data")
        warn(error)
    end

end)

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

    local success, errormessage = pcall (function()
        DATA:SetAsync(player.UserID.."-cash",player.leaderstats.Cash.Value)
    end)

    if success then
        print("Player Data successfully saved!")
    else
        print("An error occurred while saving player data")
        warn(errormessage)
    end

end)

The Script is named Script

Edit:I am getting a console error of couldn't identity player.UserId

0
You forgot an = in line 28. Also in line 21, I think you were suppose to write warn(errormessage) instead of warn(error). NordicM 77 — 3y

1 answer

Log in to vote
0
Answered by
iOwn_You 543 Moderation Voter
3 years ago

Well first, as previously mentioned you forgot = on line 28 Another possible reason is that you dont save on BindToClose() You should look into that or the game will never save the data of the last player who leaves the server.

https://developer.roblox.com/en-us/api-reference/function/DataModel/BindToClose

Ad

Answer this question