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

xpFunction.OnServerInvoke=function(plr)
    local key = 'Player-'..plr.userId
    -- Checks if player has data that can be sent
    if xpDataStore:GetAsync(key) then
        return xpDataStore:GetAsync(key)
    else
        -- sets the xp to 0 if plr is new
        xpDataStore:SetAsync(key,(0))
    end
end

I have tried this

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

but there is an error with the "="

2 answers

Log in to vote
0
Answered by 6 years ago

Try this:

local success, message = pcall(function()
    xpFunction.OnServerInvoke = function(plr)
        -- code in here
    end
end)

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:

xpFunction.OnServerInvoke = function(plr)
    local s, ret = pcall(function()
        local key = 'Player-'..plr.UserId --use uppercase U in UserId
        if xpDataStore:GetAsync(key) then
            return xpDataStore:GetAsync(key)
        else
            xpDataStore:SetAsync(key, 0)
        end
    end)
    return ret
end

Answer this question