Having problems passing server command info to client

Hi there everyone!

I am trying to implement some code I found for clearing an area but I’m having a problem making it work.

The command needs to be serverside so it can be restricted but I’m just not handling passing the radius variable correctly, I think. The resource starts fine but I get scheduler errors when i try /clearall 20

Client:

RegisterNetEvent('cia:clearAll')
AddEventHandler('cia:clearAll', function(radius)
	local radius = radius
	local TRadius = radius + 0.0
	local x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))
	
	-- Props
	ClearAreaOfObjects(x, y, z, TRadius, 1)
	
	-- NPC Police
	ClearAreaOfCops(x, y, z, TRadius, 1)
	
	-- Pedestrians
	ClearAreaOfPeds(x, y, z, TRadius, 1)
	
	-- rounds/weaps
	ClearAreaOfProjectiles(x, y, z, TRadius, 1)
	
	-- Vehicles
	ClearAreaOfVehicles(x, y, z, TRadius, false, false, false, false, false)
	
end)

Server:

RegisterCommand("clearall", function(source, args, rawCommand)
	TriggerClientEvent('cia:clearAll', radius)
end, true)

Can someone tell me what I’m doing wrong?

Thanks for your time!

TriggerClientEvent requires either a -1 for all clients or a playerid.

1 Like

So if I’m wanting to use the playerid then I would change it to:

	TriggerClientEvent('cia:clearAll', radius, source)

?

Switch the arguments. The id should always come first.

2 Likes

I’m getting an error on performing arithmatic on a nil value now.

command:

clearall 20

client:

RegisterNetEvent('cia:clearAll')
AddEventHandler('cia:clearAll', function(radius)
	local radius = radius
	local TRadius = radius + 0.0  -- Arith on nil value
	local x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))
	ClearAreaOfObjects(x, y, z, TRadius, 1)
	ClearAreaOfCops(x, y, z, TRadius, 1)
	ClearAreaOfPeds(x, y, z, TRadius, 1)
	ClearAreaOfProjectiles(x, y, z, TRadius, 1)
	ClearAreaOfVehicles(x, y, z, TRadius, false, false, false, false, false)
	TriggerEvent('chatMessage', "^1Area cleared^2")
end)

server:


RegisterCommand("clearall", function(source, args, rawCommand)
	TriggerClientEvent('cia:clearAll', source, radius)
end, true)

Error:

Error running system event handling function for resource clearitall: citizen:/scripting/lua/scheduler.lua:41: Failed to execute thread: client.lua:5: attempt to perform arithmetic on a nil value (local 'radius')

Line 5:

	local TRadius = radius + 0.0

What is radius on the server side?

RegisterCommand("clearall", function(source, args, rawCommand)
	TriggerClientEvent('cia:clearAll', source, radius)
end, true)

Because radius is nothing from the displayed code.

1 Like

Hi there Faxes, thanks for the help!

I’m trying to pass it as part of the command (clearall 20 would make radius 20). How would I get it to pass that to the client as radius?

RegisterCommand("clearall", function(source, args, rawCommand)
        local radius = tonumber(args[1])
        if radius then
	     TriggerClientEvent('cia:clearAll', source, radius)
        else
             TriggerClientEvent("chatMessage", source, "Please specify a radius.")
      end
end)
1 Like

You’re missing an end there.

1 Like
RegisterCommand("clearall", function(source, args, rawCommand)
        local radius = tonumber(args[1])
        if radius then
	     TriggerClientEvent('cia:clearAll', source, radius)
        else
             TriggerClientEvent("chatMessage", source, "Please specify a radius.")
       end
end)

Very nice, thanks so much!

2 Likes