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

How do I wrap a RemoteFunction with a pcall?[SOLVED]

Asked by 6 years ago
Edited 6 years ago

I have no clue on how to wrap this with a pcall

01xpFunction.OnServerInvoke=function(plr)
02    local key = 'Player-'..plr.userId
03    -- Checks if player has data that can be sent
04    if xpDataStore:GetAsync(key) then
05        return xpDataStore:GetAsync(key)
06    else
07        -- sets the xp to 0 if plr is new
08        xpDataStore:SetAsync(key,(0))
09    end
10end

I have tried this

01pcall(xpFunction.OnServerInvoke=function(plr)
02    local key = 'Player-'..plr.userId
03    -- Checks if player has data that can be sent
04    if xpDataStore:GetAsync(key) then
05        return xpDataStore:GetAsync(key)
06    else
07        -- sets the xp to 0 if plr is new
08        xpDataStore:SetAsync(key,(0))
09    end
10end)

but there is an error with the "="

2 answers

Log in to vote
0
Answered by 6 years ago

Try this:

1local success, message = pcall(function()
2    xpFunction.OnServerInvoke = function(plr)
3        -- code in here
4    end
5end)

Hope this helps and have a great day scripting!

0
You can change success and message to be whatever you like. User#21908 42 — 6y
Ad
Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

It's fairy simple. Do it like this:

01xpFunction.OnServerInvoke = function(plr)
02    local s, ret = pcall(function()
03        local key = 'Player-'..plr.UserId --use uppercase U in UserId
04        if xpDataStore:GetAsync(key) then
05            return xpDataStore:GetAsync(key)
06        else
07            xpDataStore:SetAsync(key, 0)
08        end
09    end)
10    return ret
11end

Answer this question