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

[SOLVED]What do I do when my pcall function doesnt return anything in the output?

Asked by 5 years ago
Edited 5 years ago

I could swear that I did everything right, its just that my pcall function wont return anything in the output and it is not saving. Take a look

Server Script inside ServerScriptService

I'm trying to save a bool value's value by changing the value then saving it. So the value starts out as true but it changes to false then it is supposed to save.

The objective is to save the value while it is changed to false, so I can rejoin the game and the value stays false for my specific key id. :)

Also I don't test in studio, I always test in the real game.

local  DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("SavePromptValue")

game.Players.PlayerAdded:Connect(function(player)
    local ValueNeedingSaving = player:WaitForChild("ValueNeedingSaving",1) -- this bool value is cloned into the player through a different script. It is set to true originally

    ValueNeedingSaving.Value = DataStore:GetAsync(player.UserId) or true -- Loading the value's value from last saved(since the start value is true, I am returning it to true again instead of false)

    if  ValueNeedingSaving.Value == false then
        print("it  worked!")
    else
        ValueNeedingSaving.Value = false -- this changes the value to false if it isn't already, so we only want to get this message once
        print("Either you are changing the value for the first time, or it didn't work")
    end

    local success, err = pcall(function()
        DataStore:SetAsync(player.UserId, ValueNeedingSaving.Value)
    end)

    if success then
        print("Data successfully saved") -- this isn't printing in the output
    end

    if err then
        print("There were some issues while saving the data") -- this is also not saving so Im confused
    end

end)

0
Success is either true or false so you could add an else inside of that if-statement to actually see if it's returning. The returned "err" gives you a message if an error occurs so you could print() that instead of checking for it. You should also SetAsync in a PlayerRemoving. xPolarium 1388 — 5y
0
try `return DataStore:SetAsync(player.UserId, ValueNeedingSaving.Value)` line 17 EpicMetatableMoment 1444 — 5y
0
Hi, i cant help xd sry, plz dont b mad mewant_taco 17 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Thanks to pidgey, he helped me figure out the answer! heres the code:

local  DSS = game:GetService("DataStoreService")
local DataStore = DSS:GetDataStore("SavePromptValue")

game.Players.PlayerAdded:Connect(function(player)
    local ValueNeedingSaving = player:WaitForChild("ValueNeedingSaving",1) -- this bool value is cloned into the player through a different script. It is set to true originally

    ValueNeedingSaving.Value = DataStore:GetAsync(player.UserId) and nil and true and DataStore:GetAsync(player.UserId) -- Loading the value's value from last saved(since the start value is true, I am returning it to true again instead of false)

    if  ValueNeedingSaving.Value == false then
        print("it  worked!")
    else
        ValueNeedingSaving.Value = false -- this changes the value to false if it isn't already, so we only want to get this message once
        print("Either you are changing the value for the first time, or it didn't work")
    end

    local success, err = pcall(function()
        DataStore:SetAsync(player.UserId, ValueNeedingSaving.Value)
    end)

    if success then
        print("Data successfully saved") -- this isn't printing in the output
    end

    if err then
        print("There were some issues while saving the data") -- this is also not saving so Im confused
    end

end)
Ad

Answer this question