Jump to content

onClientPlayerWeaponFire getElementType got nil


thund3rbird23

Recommended Posts

Bad argument @ 'getElementType' [Expected element at argument 1, got nil]

function cameraFireing(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement)
    if (getElementType(hitElement) == "player" and hitElement ~= source) then
        if getElementData(source, "char.inMarkerZone") then
            triggerServerEvent("sendGroupMessage", root, "#ffffffThe camera: #ca5454(" .. getElementData(hitElement, "char.MarkerZoneName") .. ") #ffffffdetected shots.")
        end
    end
end
addEventHandler("onClientPlayerWeaponFire", root, cameraFireing)
Link to comment
function cameraFireing(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement)
	if isElement(hitElement) then
		if (getElementType(hitElement) == "player" and hitElement ~= source) then
			if getElementData(source, "char.inMarkerZone") then
				triggerServerEvent("sendGroupMessage", root,"#ffffffThe camera: #ca5454("..getElementData(hitElement,"char.MarkerZoneName")..") #ffffffdetected shots.")
			end
		end
	end
end
addEventHandler("onClientPlayerWeaponFire", root, cameraFireing)

 

Link to comment
  • Moderators
6 hours ago, thund3rbird23 said:

Then get nothing in return, no errros and no triggerServerEvent because the hitElement is nil but I don't know why.

As your code is programmed now, you have to hit another player in order to trigger the server event.

Are you sure you have met that condition?

 

Also attach the eventhandler to the localPlayer:

addEventHandler("onClientPlayerWeaponFire", localPlayer, cameraFireing)

Even though this event is fired on each client/player. The event is also fired for each player when attached to the root.

When there are 5 players in the server, 1 player hits another player, the triggerServerEvent is activated max 5 times.

 

Edited by IIYAMA
Link to comment

Well, I want to detect if someone in the marker is shooting or killing someone then send a message to the police group. I have a few positions and if the player is in these positions then I set element data "char.isZoneCamera, true".

I know it's possible to do that, I had a script few years ago and that works well, but I lost that script so now I want to make one like that.

local positions = {
  	{1528.837890625, -1674.6318359375, 13.3828125, 50, "Police"},
  	{1304.6455078125, -1700.59375, 13.546875, 50, "Cinema"},
  	{1548.044921875, -1789.8291015625, 13.546875, 50, "Near City Hall"},
  	{1477.966796875, -1759.06640625, 13.58437538147, 50, "City hall"},
  	{1192.4443359375, -1322.03125, 13.3984375, 50, "Hospital"}
}

for k,v in ipairs(positions) do
   local x,y,z = v[1], v[2], v[3]
   local marker = createMarker(x,y,z-1, "cylinder", v[4], 255,255,255,0)
   setElementData(marker, "marker.ZoneName", v[5])
   setElementData(marker, "marker.isZoneCamera", true)
end

--- detect shots ---

function cameraFireing(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement)
	if isElement(hitElement) then
		if (getElementType(hitElement) == "player" and hitElement ~= source) then
			if getElementData(source, "char.inMarkerZone") then
				triggerServerEvent("sendGroupMessage", root,"#ffffffThe camera: #ca5454("..getElementData(hitElement,"char.MarkerZoneName")..") #ffffffdetected shots.")
			end
		end
	end
end
addEventHandler("onClientPlayerWeaponFire", root, cameraFireing)

--- detect kills ---

function cameraKilling(killerElement, weapon, bodypart)
    if (getElementType(source) == "player") then
        if getElementData(source, "char.inMarkerZone") then
            triggerServerEvent("sendGroupMessage", root, "#ffffffThe camera: #ca5454(" .. getElementData(source, "char.MarkerZoneName") .. ") #ffffffdetected a kill.")
        end
    end
end
addEventHandler("onClientPlayerWasted", getLocalPlayer(), cameraKilling)

 

Link to comment
  • Moderators
40 minutes ago, thund3rbird23 said:

if someone in the marker is shooting or killing someone

Conditions are used for excluding actions. Which is not what you want, when you want: shooting OR killing

 

Instead you want to have a variable value which only exist when all conditions are met.

local victim = isElement(hitElement) and getElementType(hitElement) == "player" and hitElement or false

 

You can't hit yourself with your own bullets:

hitElement ~= source

 


 

As I said before:

addEventHandler("onClientPlayerWeaponFire", root, cameraFireing)

 

addEventHandler("onClientPlayerWeaponFire", localPlayer, cameraFireing)

 


 

triggerServerEvent("sendGroupMessage", resourceRoot, hitElement)

 

Do this on serverside:

if hitElement then
	local message = "#ffffffThe camera: #ca5454("..getElementData(hitElement,"char.MarkerZoneName")..") #ffffffdetected shots."
end

 


 

 

addEventHandler("onClientPlayerWasted", getLocalPlayer(), cameraKilling)

onClientPlayerWasted, so this is not needed:

if (getElementType(source) == "player") then

 

 

Edited by IIYAMA
Link to comment
4 hours ago, IIYAMA said:

Conditions are used for excluding actions. Which is not what you want, when you want: shooting OR killing

 

Instead you want to have a variable value which only exist when all conditions are met.


local victim = isElement(hitElement) and getElementType(hitElement) == "player" and hitElement or false

 

You can't hit yourself with your own bullets:

hitElement ~= source

 


 

As I said before:

addEventHandler("onClientPlayerWeaponFire", root, cameraFireing)

 


addEventHandler("onClientPlayerWeaponFire", localPlayer, cameraFireing)

 


 


triggerServerEvent("sendGroupMessage", resourceRoot, hitElement)

 

Do this on serverside:


if hitElement then
	local message = "#ffffffThe camera: #ca5454("..getElementData(hitElement,"char.MarkerZoneName")..") #ffffffdetected shots."
end

 


 

 


addEventHandler("onClientPlayerWasted", getLocalPlayer(), cameraKilling)

onClientPlayerWasted, so this is not needed:

if (getElementType(source) == "player") then

 

 

This is return nothing. No errors in debug and no chatbox message:

function cameraFireing(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement)
       local victim = isElement(hitElement) and getElementType(hitElement) == "player" and hitElement or false
	   if victim then
			if getElementData(source, "char.inMarkerZone") then
				outputChatBox("test")
			end
		end
end
addEventHandler("onClientPlayerWeaponFire", localPlayer, cameraFireing)

 

Link to comment
  • Moderators

 

5 hours ago, IIYAMA said:

Conditions are used for excluding actions. Which is not what you want, when you want: shooting OR killing

 

if victim then

So, do not add this clientside.

 

 

But as I said in my previous post:

5 hours ago, IIYAMA said:

Do this on serverside:


    if hitElement then
    	local message = "#ffffffThe camera: #ca5454("..getElementData(hitElement,"char.MarkerZoneName")..") #ffffffdetected shots."
    end

 

 


 

And add WAY more debug lines. You have now just 1 line, which is in this case your outputChatBox. That is not enough!

YOU ARE A GOD, YOU WANT TO DOMINATE, YOU WANT TO BE IN CONTROL AND KNOW EVERYTHING!

 

function cameraFireing(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement)
	iprint("event is fired")
	if getElementData(source, "char.inMarkerZone") then
		iprint("source is in zone")	
	end
end

 

Edited by IIYAMA
Link to comment
1 hour ago, IIYAMA said:

 

 


if victim then

So, do not add this clientside.

 

 

But as I said in my previous post:

 


 

And add WAY more debug lines. You have now just 1 line, which is in this case your outputChatBox. That is not enough!

YOU ARE A GOD, YOU WANT TO DOMINATE, YOU WANT TO BE IN CONTROL AND KNOW EVERYTHING!

 


function cameraFireing(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement)
	iprint("event is fired")
	if getElementData(source, "char.inMarkerZone") then
		iprint("source is in zone")	
	end
end

 

Ok, I get "event is fired" and "source is in zone" when I firing in the marker (positions)

And where I do place these in server side?:
 

local victim = isElement(hitElement) and getElementType(hitElement) == "player" and hitElement or false

 

 if hitElement then
    	local message = "#ffffffThe camera: #ca5454("..getElementData(hitElement,"char.MarkerZoneName")..") #ffffffdetected shots."
    end

Or how to call/reference it for these if I want to output the message when I fire in the marker?

Link to comment
  • Moderators
26 minutes ago, thund3rbird23 said:

Or how to call/reference it for these if I want to output the message when I fire in the marker?

The variable victim contains extra information. It is optional.

 

function cameraFireing(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement)
	iprint("event is fired")
	if getElementData(localPlayer, "char.inMarkerZone") then
		iprint("source is in zone")
		local victim = isElement(hitElement) and getElementType(hitElement) == "player" and hitElement or false
		local zoneName = getElementData(localPlayer,"char.MarkerZoneName")
		triggerServerEvent("sendGroupMessage", resourceRoot, victim, victim and getPlayerName(victim), zoneName)
	end
end
addEventHandler("onClientPlayerWeaponFire", localPlayer, cameraFireing)

 

 

addEvent("sendGroupMessage", true)
addEventHandler("sendGroupMessage", resourceRoot, 
function (victim, victimName, zoneName) 
    
	local message = "#ffffffThe camera: #ca5454(".. zoneName ..") #ffffffdetected shots. "
	if victimName then
		message = message .. " You hit this player: " .. victimName
	end
    
	outputChatBox(message, client)
    
	if isElement(victim) then
		outputChatBox(getPlayerName(client) .. " hits you and he doesn't feel sorry for it.", victim)
	end
    
   
	local players = getElementsByType("player")
	for i=1, #players do
		local player = players[i]
		if player ~= client and player ~= victim then
			outputChatBox(getPlayerName(client) .. " loves sheeps.", player)
		end
	end
end, false)

 

Edited by IIYAMA
  • Thanks 1
Link to comment
36 minutes ago, IIYAMA said:

The variable victim contains extra information. It is optional.

 


function cameraFireing(weapon, ammo, ammoInClip, hitX, hitY, hitZ, hitElement)
	iprint("event is fired")
	if getElementData(localPlayer, "char.inMarkerZone") then
		iprint("source is in zone")
		local victim = isElement(hitElement) and getElementType(hitElement) == "player" and hitElement or false
		local zoneName = getElementData(localPlayer,"char.MarkerZoneName")
		triggerServerEvent("sendGroupMessage", resourceRoot, victim, victim and getPlayerName(victim), zoneName)
	end
end
addEventHandler("onClientPlayerWeaponFire", localPlayer, cameraFireing)

 

 


addEvent("sendGroupMessage", true)
addEventHandler("sendGroupMessage", resourceRoot, 
function (victim, victimName, zoneName) 
    
	local message = "#ffffffThe camera: #ca5454(".. zoneName ..") #ffffffdetected shots. "
	if victimName then
		message = message .. " You hit this player: " .. victimName
	end
    
	outputChatBox(message, client)
    
	if isElement(victim) then
		outputChatBox(getPlayerName(client) .. " hits you and he doesn't feel sorry for it.", victim)
	end
    
   
	local players = getElementsByType("player")
	for i=1, #players do
		local player = players[i]
		if player ~= client and player ~= victim then
			outputChatBox(getPlayerName(client) .. " loves sheeps.", player)
		end
	end
end, false)

 

I just needed the cameraFireing function the other parts are already done and now works perfect together with your fix. Thank you!

  • 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...