Jump to content

callServerFunction


roaddog

Recommended Posts

So I use this useful function CallServerFunction

server:

-- Always, always, always, (always) use a list of allowed servers functions. 
-- Otherwise hackers can call any function and destroy your server. 
allowedFunctions = { ["isPlayerInACL"]=true } 
  
function callServerFunction(funcname, ...) 
    if not allowedFunctions[funcname] then 
        -- Protect server from abuse 
        outputServerLog( "SECURITY: " .. tostring(getPlayerName(client)) .. " tried to use function " .. tostring(funcname) ) 
        return 
    end 
  
    local arg = { ... } 
    if (arg[1]) then 
        for key, value in next, arg do arg[key] = tonumber(value) or value end 
    end 
    loadstring("return "..funcname)()(unpack(arg)) 
end 
addEvent("onClientCallsServerFunction", true) 
addEventHandler("onClientCallsServerFunction", resourceRoot , callServerFunction) 

client:

function callServerFunction(funcname, ...) 
    local arg = { ... } 
    if (arg[1]) then 
        for key, value in next, arg do 
            if (type(value) == "number") then arg[key] = tostring(value) end 
        end 
    end 
    -- If the serverside event handler is not in the same resource, replace 'resourceRoot' with the appropriate element 
    triggerServerEvent("onClientCallsServerFunction", resourceRoot , funcname, unpack(arg)) 
end 

My function:

---Client 
function isPlayerStaff ( p ) 
    local p = p or getLocalPlayer() 
    if ( p and getElementType ( p ) == 'player' ) then 
        if ( callServerFunction("isPlayerInACL", p, "Level 1" ) ) then 
            return true 
        end 
        return false 
    end 
end 
--Server 
function isPlayerInACL ( player, acl ) 
    local account = getPlayerAccount ( player ) 
    if ( isGuestAccount ( account ) ) then 
        return false 
    end 
    return isObjectInACLGroup ( "user."..getAccountName ( account ), aclGetGroup ( acl ) ) 
end 
  

This code returns nothing. I don't know why but it should return true @isPlayerInACL.

Link to comment

The point is, it will stay returning what it returns.

Well loadstring loads a function and its arguements from string provided.

Thats the scheme how its done

Client (callServerFunction('checkACL',....)) -> server (check some things and let's return with callClientFunction('asnwerACL',....)) -> Client

Link to comment
function isPlayerStaff ( p ) will always return true

You can not check this way, you must callServerFunction and then callback callClientFunction with an answer.

No, it will always returns false because the function callServerFunction doesn't return anything -> nil

Link to comment

isPlayerStaff will either return nil (won't return anything in fact) or return false, because callServerFunction will always return nil, so the check will fail and end up returning false. If the first check fails, it will return nil.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...