Jump to content

Need help with safezone script


pearlyhell

Recommended Posts

first of all its my firs topic in forums.

I'm having problem with my safezone script which i wrote all of it (nearly, took hints from wiki)

the problem is, when i enter the zone, it says 'you entered safezone' , but after a few seconds, 'you left the safezone' text appears. I couldnt solve the problem with my knowledge so im looking for help.

only problem is; it says i left safezone after a few seconds, other functions working fine.

 

clientside:

	
safezones = {
    {1578.2421875, 1801.248046875, 10.8203125, 58, 61, 100},
    {1996.958984375, -1450.9560546875, 13.5546875, 100, 55, 100},
    {-2740.775390625, 577.9326171875, 14.5546875, 150, 100, 100},
}
	function createthings ()
    for i, v in ipairs (safezones) do
        x, y, z, w, d, h = unpack(v)
        safezone = createColCuboid(x, y, z - 30, w, d, h)
        radar = createRadarArea(x, y, w, d, 0, 200, 0, 50)
        addEventHandler("onClientColShapeHit", safezone, hit)
        addEventHandler("onClientColShapeLeave", safezone, Leave)
    end
end
addEventHandler("onClientResourceStart", resourceRoot, createthings)
	function no()
    cancelEvent ()
end
	function disableproof()
    removeEventHandler ("onPlayerDamage",localPlayer,no)
end
	
function hit(element)
if source == safezone then
setPedWeaponSlot (element,0)
if (getElementType (element) == "vehicle") then
    exports.GTWvehicleshop:saveAndRemoveVehicle(element,true)
    exports.GTWtopbar:dm("You can't drive vehicle inside safezone",200,50,50)
    toggleControl ("next_weapon",false)
    toggleControl ("previous_weapon",false)
    toggleControl ("fire",false)
    addEventHandler ("onPlayerDamage",localPlayer,no)
    addEventHandler("onPedWasted", localPlayer, disableproof)
elseif (getElementType (element) == "player") then
    exports.GTWtopbar:dm("You entered the safezone, all weapons are forbidden.",200,50,50)
    toggleControl ("next_weapon",false)
    toggleControl ("previous_weapon",false)
    toggleControl ("fire",false)
    addEventHandler ("onPlayerDamage",localPlayer,no)
    addEventHandler("onPedWasted", localPlayer, disableproof)
end
end
end
	function Leave(element)
if source == safezone then
    exports.GTWtopbar:dm("You left the safezone.",200,50,50)
    toggleControl ("next_weapon",true)
    toggleControl ("previous_weapon",true)
    toggleControl ("fire",true)
    removeEventHandler ("onPlayerDamage",localPlayer,no)
end
end
 
	
Edited by pearlyhell
Link to comment

You are coding a client code, so why you are using onPedWasted and  onPlayerDamage events ? they are server sided events only which that mean they will not work in client side.

Note: Client ( functions, events ) cannot be merged with Server side ( functions, events ).

Edited by KariiiM
Link to comment
10 hours ago, KariiiM said:

You are coding a client code, so why you are using onPedWasted and  onPlayerDamage events ? they are server sided events only which that mean they will not work in client side.

Note: Client ( functions, events ) cannot be merged with Server side ( functions, events ).

thanks for reply, im new at scripting and didnt know onPlayerDamage and onPedWasted, i will replace them. but i dont think my problem is related with theese functions. when i enter my safezone, it sets my weapon to fist, so there seems no problem with entering. but after a few seconds it says 'you left the safezone' , which is the real problem. it toggles my controls back to true and acts like i left the zone, but im still in the zone.

Link to comment

NOTE: Code is server sided only.

local safeZonesTable = 
	{
		{ x = 1578.2421875, y = 1801.248046875, z = 10.8203125, width = 58, depth = 61, height = 100 },
		{ x = 1996.958984375, y = -1450.9560546875, z = 13.5546875, width = 100, depth = 55, height = 100 },
		{ x = -2740.775390625, y = 577.9326171875, z = 14.5546875, width = 150, depth = 100, height = 100 }
	}

local controls = { "fire", "next_weapon", "previous_weapon" }
	
addEventHandler ( "onResourceStart", resourceRoot,
	function ( )
		for _, zone in ipairs ( safeZonesTable ) do 
			local safeZone = createColCuboid ( zone.x, zone.y, zone.z - 30, zone.width, zone.depth, zone.height )
			createRadarArea ( zone.x, zone.y, zone.width, zone.depth, 0, 200, 0, 50 )
			addEventHandler ( "onColShapeHit", safeZone, elementHitSafeZone )
			addEventHandler ( "onColShapeLeave", safeZone, elementLeaveSafeZone )
		end
	end
)
 
function elementHitSafeZone ( theElement, mDim )
	if ( not mDim ) then 
		return 
	end 
	
	if isElement ( theElement ) then 
		if ( getElementType ( theElement ) == "player" ) then 
			for _, control in ipairs ( controls ) do  
				toggleControl ( control, false ) 
			end
			exports.GTWtopbar:dm ( "You entered the safezone, all weapons are forbidden.", theElement, 200, 50, 50 )
		elseif ( getElementType ( theElement ) == "vehicle" ) then
			for _, control in ipairs ( controls ) do 
				toggleControl ( control, false ) 
			end
			exports.GTWvehicleshop:saveAndRemoveVehicle ( theElement, true )
			
			local thePlayer = getVehicleController ( theElement )
			if ( thePlayer ) then 
				exports.GTWtopbar:dm ( "You can't drive vehicle inside safezone", thePlayer, 200, 50, 50 )
			end
		end 
	end
end 

function elementLeaveSafeZone ( theElement, mDim )
	if ( not mDim ) then 
		return 
	end 
	
	if ( isElement ( theElement ) and getElementType ( theElement ) == "player" ) then 
		for _, control in ipairs ( controls ) do 
			toggleControl ( control, true ) 
		end
		exports.GTWtopbar:dm ( "You left the safezone.", theElement, 200, 50, 50 )
	end
end

I re made it for you, I coded it without testing it, so if you find any error, inform me here. and tell me the result after trying the code.

good luck.

Link to comment

Fixed some mistakes

local safeZonesTable = {
	{ x = 1578.2421875, y = 1801.248046875, z = 10.8203125, width = 58, depth = 61, height = 100 },
	{ x = 1996.958984375, y = -1450.9560546875, z = 13.5546875, width = 100, depth = 55, height = 100 },
	{ x = -2740.775390625, y = 577.9326171875, z = 14.5546875, width = 150, depth = 100, height = 100 }
}

local controls = { "fire", "next_weapon", "previous_weapon" }
	
addEventHandler ( "onResourceStart", resourceRoot,
	function ( )
		for _, zone in ipairs ( safeZonesTable ) do 
			local safeZone = createColCuboid ( zone.x, zone.y, zone.z - 30, zone.width, zone.depth, zone.height )
			createRadarArea ( zone.x, zone.y, zone.width, zone.depth, 0, 200, 0, 50 )
			addEventHandler ( "onColShapeHit", safeZone, elementHitSafeZone )
			addEventHandler ( "onColShapeLeave", safeZone, elementLeaveSafeZone )
		end
	end
)
 
function elementHitSafeZone ( theElement, mDim )
	if theElement and isElement(theElement) and getElementType ( theElement ) == "player" then 
		if (isPedInVehicle(theElement)) then 
			local vehicle = getPedOccupiedVehicle(theElement)
			exports.GTWvehicleshop:saveAndRemoveVehicle (vehicle, true)
		end 
			
		for _, control in ipairs ( controls ) do  
			toggleControl (theElement,control, false ) 
		end
		exports.GTWtopbar:dm ( "You entered the safezone, all weapons are forbidden.", theElement, 200, 50, 50 )
	end
end 


function elementLeaveSafeZone ( theElement, mDim )
	if theElement and isElement(theElement) and getElementType ( theElement ) == "player" then 
		for _, control in ipairs ( controls ) do 
			toggleControl (theElement,control, true ) 
		end
		exports.GTWtopbar:dm ( "You left the safezone.", theElement, 200, 50, 50 )
	end
end

 

Edited by Walid
Link to comment
53 minutes ago, Walid said:

Fixed some mistakes

 


local safeZonesTable = {
	{ x = 1578.2421875, y = 1801.248046875, z = 10.8203125, width = 58, depth = 61, height = 100 },
	{ x = 1996.958984375, y = -1450.9560546875, z = 13.5546875, width = 100, depth = 55, height = 100 },
	{ x = -2740.775390625, y = 577.9326171875, z = 14.5546875, width = 150, depth = 100, height = 100 }
}

local controls = { "fire", "next_weapon", "previous_weapon" }
	
addEventHandler ( "onResourceStart", resourceRoot,
	function ( )
		for _, zone in ipairs ( safeZonesTable ) do 
			local safeZone = createColCuboid ( zone.x, zone.y, zone.z - 30, zone.width, zone.depth, zone.height )
			createRadarArea ( zone.x, zone.y, zone.width, zone.depth, 0, 200, 0, 50 )
			addEventHandler ( "onColShapeHit", safeZone, elementHitSafeZone )
			addEventHandler ( "onColShapeLeave", safeZone, elementLeaveSafeZone )
		end
	end
)
 
function elementHitSafeZone ( theElement, mDim )
	if theElement and isElement(theElement) and getElementType ( theElement ) == "player" then 
		if (isPedInVehicle(theElement)) then 
			local vehicle = getPedOccupiedVehicle(theElement)
			exports.GTWvehicleshop:saveAndRemoveVehicle (vehicle, true)
		end 
			
		for _, control in ipairs ( controls ) do  
			toggleControl (theElement,control, false ) 
		end
		exports.GTWtopbar:dm ( "You entered the safezone, all weapons are forbidden.", theElement, 200, 50, 50 )
	end
end 


function elementLeaveSafeZone ( theElement, mDim )
	if theElement and isElement(theElement) and getElementType ( theElement ) == "player" then 
		for _, control in ipairs ( controls ) do 
			toggleControl (theElement,control, true ) 
		end
		exports.GTWtopbar:dm ( "You left the safezone.", theElement, 200, 50, 50 )
	end
end

thanks, works fine. but as i said before, how can i make people inside zone invincible ? player stands outside and shoots the one who is inside. 

Link to comment

thank you all for help.

i added setElementData to the functions 'elementHitSafeZone' and 'elementLeaveSafeZone' , shown in first code

then, with the hint of NssoR, i created a clientside file, which is shown in second code

now it functions perfectly. thanks for all replies :)

if anyone interested in full code im putting serverside file too (third code)

setElementData ( theElement, "inSafezone", true )
function donthurtme ( attacker, weapon, bodypart )
	if ( getElementData ( getLocalPlayer(), "inSafezone" ) == true ) then
		cancelEvent()
	end
end
addEventHandler ( "onClientPlayerDamage", getLocalPlayer(), donthurtme )
local safeZonesTable = 
	{
		{ x = 1578.2421875, y = 1801.248046875, z = 10.8203125, width = 58, depth = 61, height = 100 },
		{ x = 1996.958984375, y = -1450.9560546875, z = 13.5546875, width = 100, depth = 55, height = 100 },
		{ x = -2740.775390625, y = 577.9326171875, z = 14.5546875, width = 150, depth = 100, height = 100 }
  		-- you can add the coords for new safe zones, theese three are the main hospitals.
	}

local controls = { "fire", "next_weapon", "previous_weapon", "aim_weapon", "sprint" } -- you can edit the controls you want to disable
	
addEventHandler ( "onResourceStart", resourceRoot,
	function ( )
		for _, zone in ipairs ( safeZonesTable ) do 
			local safeZone = createColCuboid ( zone.x, zone.y, zone.z - 30, zone.width, zone.depth, zone.height )
			createRadarArea ( zone.x, zone.y, zone.width, zone.depth, 0, 200, 0, 50 )
			addEventHandler ( "onColShapeHit", safeZone, elementHitSafeZone )
			addEventHandler ( "onColShapeLeave", safeZone, elementLeaveSafeZone )
		end
	end
)

function elementHitSafeZone ( theElement, mDim )
	if isElement ( theElement ) then 
		if ( getElementType ( theElement ) == "player" ) then 
			for _, control in ipairs ( controls ) do  
				toggleControl ( theElement, control, false ) 
				setPlayerWeaponSlot ( theElement, 0 )
			end
			exports.GTWtopbar:dm ( "You entered the safezone.", theElement, 200, 50, 50 ) -- simply change 'exports.GTWtopbar:dm' to 'outputChatBox' for using it on chatbox
			setElementData ( theElement, "inSafezone", true )
		elseif ( getElementType ( theElement ) == "vehicle" ) then
			local thePlayer = getVehicleController ( theElement )
			for _, control in ipairs ( controls ) do 
				toggleControl ( thePlayer, control, false )
				setPlayerWeaponSlot ( thePlayer, 0 )
			end
			if ( thePlayer ) then 
				exports.GTWtopbar:dm ( "You can't drive vehicles inside the safezone", thePlayer, 200, 50, 50 )
			end
			exports.GTWvehicleshop:saveAndRemoveVehicle ( theElement, true ) -- edit or simply change it with the function destroyElement(theElement) for using it on a freeroam server etc
			setElementData ( theElement, "inSafezone", true )
		end
		
	end
end 


function elementLeaveSafeZone ( theElement, mDim )
	if ( isElement ( theElement ) and getElementType ( theElement ) == "player" ) then 
		for _, control in ipairs ( controls ) do 
			toggleControl ( theElement, control, true ) 
		end
		exports.GTWtopbar:dm ( "You left the safe zone.", theElement, 200, 50, 50 )
		setElementData ( theElement, "inSafezone", nil )
		
	end
end

have a good day.

Edited by pearlyhell
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...