Jump to content

Why explode everyone? How to fix this in script?


Turbe$Z

Recommended Posts

-- client --

function boom ( )
	local pX, pY, pZ = getElementPosition ( getLocalPlayer() )
	createExplosion ( pX+3, pY+3, pZ, 2 )
	createExplosion ( pX-3, pY-3, pZ, 2 )
	createExplosion ( pX+5, pY-2, pZ, 2 )
	createExplosion ( pX-4, pY+4, pZ, 2 )
end
addEvent( "rob", true )
addEventHandler( "rob", root, boom )

-- server --

function robbant(p,s)
if hasObjectPermissionTo(p,"command.rob") then
triggerClientEvent(getRootElement(),"rob",getRootElement())
end
end
addCommandHandler("rob",robbant)

When i type 'rob', explode everyone.. Why? How to fix this?

Edited by Turbo777
Link to comment

I can't see any error... but why are you making a clientside script if you can do all serverside?

triggerClientEvent note:

Note: To save client CPU, you should avoid setting sourceElement to the root element where possible. Using resourceRoot is usually sufficient if the event is handled by the same resource on the client.

 


EDIT: ohhh i now see the problem: you are triggering to the root, so EVERY player will get the clientside script.

Edited by LoPollo
Link to comment

Line 3 serverside:

triggerClientEvent(getRootElement(),"rob",getRootElement())

replace with

triggerClientEvent(p,"rob",getRootElement())

If you want to solve the problem said in the note of triggerClientEvent, replace with

triggerClientEvent(p,"rob",p)

 

(if you don't need the extra arguments of clientside createExplosion then it's still better to move all on serverside, i will post the code as soon as i return)

Edited by LoPollo
Link to comment

Well hasObjectPermissionTo is a server-side only function, so you either make changes to it so you don't have to use that function or just do the explosion server side:

function robbant(p,s)
	if hasObjectPermissionTo(p,"command.rob") then
		local pX, pY, pZ = getElementPosition ( p )
		createExplosion ( pX+3, pY+3, pZ, 2 )
		createExplosion ( pX-3, pY-3, pZ, 2 )
		createExplosion ( pX+5, pY-2, pZ, 2 )
		createExplosion ( pX-4, pY+4, pZ, 2 )
	end
end
addCommandHandler("rob",robbant)

 

Edited by pa3ck
  • Like 1
Link to comment

Ok here's the serverside with all in it. I never used the restricted argument in addCMDHandler, but it should work... still it's better to test. If testing of the first function result in a "fail", then use the second variant.

function nameThatFunction( playerSource, cmd, ... )
	local pX, pY, pZ = getElementPosition ( playerSource )
	createExplosion ( pX+3, pY+3, pZ, 2 )
	createExplosion ( pX-3, pY-3, pZ, 2 )
	createExplosion ( pX+5, pY-2, pZ, 2 )
	createExplosion ( pX-4, pY+4, pZ, 2 )
end
addCommandHandler( "rob", nameThatFunction, true)
--that true means the resource is restricted, so only if a player is in a group with the access to command.rob that command will be executed


--since i NEVER used the restricted argument, here's the variant with the check inside the function
function nameThatFunction( playerSource, cmd, ... )
	if hasObjectPermissionTo(playerSource,"command.rob") then
		local pX, pY, pZ = getElementPosition ( playerSource )
		createExplosion ( pX+3, pY+3, pZ, 2 )
		createExplosion ( pX-3, pY-3, pZ, 2 )
		createExplosion ( pX+5, pY-2, pZ, 2 )
		createExplosion ( pX-4, pY+4, pZ, 2 )
	end
end
addCommandHandler( "rob", nameThatFunction)

EDIT: lol i noticed that the second variant is already posted, sorry for the duplicate.

Edited by LoPollo
  • Like 1
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...