Termycraft Posted August 14, 2012 Share Posted August 14, 2012 Server-side call from Client-side, and Client-side call from Server-side Hi to everyone, I made a script that makes easy to call any server-side function from client-side and client-side functions from server-side, just copy these codes in your script, Client-side in a Client-side lua file, and Server-side in a Server-side lua file, works ONLY for one resource, here is the code: This goes in CLIENT-SIDE addEvent("executeClientFunction", true) addEventHandler("executeClientFunction", getRootElement(), function(theFunction, ...) g = {...} pcall(loadstring(theFunction)) end ) function runPlatformFunction(platform, theFunction, ...) local args = {...} theFunction = theFunction.."(" for index, argument in ipairs(args) do if index == #args then theFunction = theFunction.."g["..index.."])" else theFunction = theFunction.."g["..index.."], " end end triggerServerEvent("executeFunction", getRootElement(), platform, theFunction, ...) end This goes in SERVER-SIDE addEvent("executeFunction", true) addEventHandler("executeFunction", getRootElement(), function(platform, theFunction, ...) if platform == "server" then g = {...} local result = pcall(loadstring(theFunction)) elseif platform == "client" then triggerClientEvent("executeClientFunction", getRootElement(), theFunction, ...) end return true end ) function runClientFunction(theFunction, ...) local args = {...} theFunction = theFunction.."(" for index, argument in ipairs(args) do if index == #args then theFunction = theFunction.."g["..index.."])" else theFunction = theFunction.."g["..index.."], " end end triggerEvent("executeFunction", getRootElement(), "client", theFunction, ...) end How to use in Client-side to call client and server functions You can use this function in client-side: runPlatformFunction(platform, theFunction, arg1, arg2, ...) platform can be "server" or "client", if it's server, the function will be executed in server-side, if it's "client", the function will be executed in client-side by everyone, so every player can see the "createFire", for example.theFunction here goes the function between the brackets, for example "setElementVelocity", ONLY the function, without arguments.arg1, arg2, ... here go the arguments, WITHOUT brackets, for example hitElement, velX, velY, velZ. If the function is "outputChatBox" or any function that requires a string, you can use the brackets, (ex. "Hi, how are you?") but only where the string is required.Ex. runPlatformFunction("server", "setElementVelocity", hitElement, velX, velY, velZ) -> calls the server-side function from a client.Ex. runPlatformFunction("client", "createFire", 215, 226, getGroundPosition(215, 226, 600)) -> calls the "createFire" client function from a client but will be executed by everyone: everyone can see the fire. How to use in Server-side to call a synchronized Client-side function This is the code: runClientFunction(theFunction, arg1, arg2, ...) Works similar with the other function, but this can be called in Server-side only and calls a synchronized between clients client-side function.Ex. runClientFunction("playSound", "sound.wav") -> Makes every client play "sound.wav". Example: Super Punch This is a cool mix of client-side functions and server-side functions and shows you the power of this code, here is the script: bindKey("fire", "down", function() local weapon = getPedWeapon(getLocalPlayer()) if weapon == 0 then local x, y, z = getElementPosition(getLocalPlayer()) local rot = getPedRotation(getLocalPlayer()) rot = rot + 90 rot = math.rad(rot) local vx, vy = x + math.cos(rot), y + math.sin(rot) local collision, hitX, hitY, hitZ, hitElement = processLineOfSight(x, y, z, vx, vy, z, true, true, true, true, true, false, false, false, getLocalPlayer()) if collision == true and isElement(hitElement) == true then if getElementType(hitElement) == "vehicle" then local velX, velY, velZ = math.cos(rot) * 0.5, math.sin(rot) * 0.5, 0.5 runPlatformFunction("server", "setElementVelocity", hitElement, velX, velY, velZ) runPlatformFunction("server", "setElementHealth", hitElement, getElementHealth(hitElement) - 250) for i = 0, 5 do runPlatformFunction("server", "setVehicleDoorState", hitElement, i, 4) end end end end end ) When you press the "fire" button, if there is a collision match with the vehicle in front of you, the script will call the server-side functions "setElementVelocity" to kick out the vehicle, "setElementHealth" to remove some health and "setVehicleDoorState" to remove all doors from the vehicle. Link to comment
Guest Guest4401 Posted August 14, 2012 Share Posted August 14, 2012 Interesting, but something similar existed before. callServerFunction callClientFunction Link to comment
TAPL Posted August 14, 2012 Share Posted August 14, 2012 This useless, it's won't return anything. Link to comment
Callum Posted August 14, 2012 Share Posted August 14, 2012 Any server using these functions will be very prone to being hacked by modified clients. This useless, it's won't return anything. You cannot get a return value for servr/client, as they are latent. If you waited for a return, it would halt execution. 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