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

Data storage saved. Script Print Error when not saved. What is wrong with the script?

Asked by 4 years ago

If data is saved then it will print error. My script print Error. It should have saved. what did I do wrong

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

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

    local points = Instance.new("IntValue", leaderstats)    
        points.Name = "Points"
        local PlayerID = "player"..player.UserId

        local PlayerData
        local success, errormessage = pcall(function()
        PlayerData = PlayerDataStore:GetSync(PlayerID)

        end)

        if success then
            points.Value = PlayerData
        end

end)

game.Players.PlayerRemoving:Connect(function(player)
    local PlayerID = "player"..player.UserId
    local data = player.leaderstats.Points.Value
    local success, errormessage = pcall(function()
            PlayerDataStore:SetSync(PlayerID, data)
    end)


    if success then 
        print ("data saved")
    else
            print ("error")
    end
end)
0
Hmmm still error... HegemoneXT -45 — 4y
0
Hmm.... I added print after the first function and added print after the second function and it all ran, meaning that there is nothing wrong with my script, yet still it did not print "data saved" or "error" HegemoneXT -45 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

Howdy!

It's :SetAsync(), not :SetSync(). Look at line 14 and 28. You can read more about this here.

If this helped you out, consider accepting this answer for those sweet, sweet reputation points. If not, comment below and I (or someone else) will help you out.

Be sure to check out the Roblox API Documentation as well for additional reference.

0
lol thanks HegemoneXT -45 — 4y
0
error still does not work HegemoneXT -45 — 4y
0
it doesn't print anything now HegemoneXT -45 — 4y
0
Weird, are you getting any error in the Output? TaxesArentAwesome 514 — 4y
0
no HegemoneXT -45 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

This is what it says

--[[
    Calls a function and throws an error if it attempts to yield.

    Pass any number of arguments to the function after the callback.

    This function supports multiple return; all results returned from the
    given function will be returned.
]]

local function resultHandler(co, ok, ...)
    if not ok then
        local message = (...)
        error(debug.traceback(co, message), 2)
    end

    if coroutine.status(co) ~= "dead" then
        error(debug.traceback(co, "Attempted to yield inside changed event!"), 2)
    end

    return ...
end

local function NoYield(callback, ...)
    local co = coroutine.create(callback)

    return resultHandler(co, coroutine.resume(co, ...))
end

return NoYield

Answer this question