robod Posted December 19, 2017 Share Posted December 19, 2017 What I'm doing wrong? Client: if player == localPlayer then setTimer( function() callServer( 'createDropPicukup', wepons_models[ weapon ], x1, y1, z1, p_rot, slot, ammo, ammoc ) end, _dropSystem[ pickup ].time +50, 1 ) end Server: function callServer(funcname, ...) 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("on_server_call", true) addEventHandler("on_server_call", resourceRoot, callServer) function createDropPicukup ( id, x, y, z, rot, slot, ammo, ammoc ) outputChatBox ( "Red #FFFFFFWhite", getRootElement(), 255, 0, 0, true ) local object = createObject ( id, x, y, z, 87, 0, rot ) setElementCollisionsEnabled ( object, false ) local marker = createMarker ( x, y, z+0.25 + math.random( 0.0500, 0.1005 ) , "corona", 1.3, color_fromSlot[ slot ].r or 255, color_fromSlot[ slot ].g or 255, color_fromSlot[ slot ].b or 255, 12.5, getRootElement() ) setElementData( marker, 'pWeapon', { getIDFromModel( id ), ammo , ammoc } ) attachElements ( marker, object ) setElementParent( object, marker ) end Link to comment
quindo Posted December 19, 2017 Share Posted December 19, 2017 You can't call server functions from client side, those are 2 separate environments, callServers is availible only on the server side, not on client. Link to comment
robod Posted December 19, 2017 Author Share Posted December 19, 2017 6 minutes ago, quindo said: You can't call server functions from client side, those are 2 separate environments, callServers is availible only on the server side, not on client. How can I call server from client then? I tried this https://wiki.multitheftauto.com/wiki/CallServerFunction And there is example: -- get the local player element local _local = getLocalPlayer() -- define the leaveTeam command handler function function cmdLeaveTeam() -- set the player's team to nil callServerFunction("setPlayerTeam", _local) end -- add the command handler addCommandHandler("leaveTeam", cmdLeaveTeam, false) callserver on serverside is non sense Link to comment
quindo Posted December 19, 2017 Share Posted December 19, 2017 Like this: 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 if player == localPlayer then setTimer( function() callServerFunction( 'createDropPicukup', wepons_models[ weapon ], x1, y1, z1, p_rot, slot, ammo, ammoc ) end, _dropSystem[ pickup ].time +50, 1 ) end Server allowedFunctions = { ["createDropPicukup"]=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) function createDropPicukup ( id, x, y, z, rot, slot, ammo, ammoc ) outputChatBox ( "Red #FFFFFFWhite", getRootElement(), 255, 0, 0, true ) local object = createObject ( id, x, y, z, 87, 0, rot ) setElementCollisionsEnabled ( object, false ) local marker = createMarker ( x, y, z+0.25 + math.random( 0.0500, 0.1005 ) , "corona", 1.3, color_fromSlot[ slot ].r or 255, color_fromSlot[ slot ].g or 255, color_fromSlot[ slot ].b or 255, 12.5, getRootElement() ) setElementData( marker, 'pWeapon', { getIDFromModel( id ), ammo , ammoc } ) attachElements ( marker, object ) setElementParent( object, marker ) end Look that wiki source shows both source that should've been inserted into client and server side 1 Link to comment
robod Posted December 19, 2017 Author Share Posted December 19, 2017 nice it works perfectly Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now