You’ll want declare speedZone at the top of your script outside of the function and command so that it remains inscope to be used when the command attempts to remove it later. Optimally you’d also declare the blip variable here as well.

In your command you want to store the integer returned by AddSpeedZoneForCoord to the speedZone variable that can then be used to remove the speed zone on future runs of the command. Same thing can happen with the blip here, RemoveBlip(blip), RemoveSpeedZone(speedZOne).

Also the nativedb is recommending you use ‘false’ in AddSpeedZoneForCoord, though I’ve never actually used this native before, I’m not sure if it even works :slight_smile:

[EDIT] - Tested out of curiosity and this native does work, slowed down traffic on the highway to 5mph with the below. Running the command a second time makes traffic speed back up to normal. This could be very useful for some scripts like cops at a traffic scene, etc. Cool.

client.lua

local speedZoneActive = false
local blip
local speedZone

RegisterCommand('speedzone', function(source, args, raw)
	if speedZone then
		RemoveBlip(blip)
		RemoveSpeedZone(speedZone)
		speedZoneActive = false
	else
		speedZoneActive = true
		local x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1)))
		blip = AddBlipForRadius(x, y, z, 50.0)
		SetBlipColour(blip,idcolor)
        SetBlipAlpha(blip,80)
		SetBlipSprite(blip,9)
		speedZone = AddSpeedZoneForCoord(x, y, z, 50.0, 2.2352, false)
	end
end, false)
3 Likes