Jump to content

Fuel problem with spectating a refilling player


Stroth

Recommended Posts

Hello, I have a little problem with a fuel system wich I can't fix.

In my map I have a location where players can refill their tank.

If player-1 ran out of fuel (or have less fuel), he can spectate. But If he's spectating a player (for example player-2) at the fuel station, and player-2 is refilling his tank, player-1 also gets fuel (because he's spectating a player which is refilling his tank).

 

How can I stop spectators refill their tank if they spectate a player which is refilling?

 

My code:

function fuelRefill()
    if not isPedInVehicle(localPlayer) then return end
    for i,marker in ipairs(refill_stations) do
        local p_veh = getPedOccupiedVehicle(localPlayer)
        if isElementWithinMarker(p_veh, marker) and fuel < 100 then
            fuel = fuel + r_rate
        end
    end
end

 

Link to comment
48 minutes ago, myonlake said:

Well, how do you define someone a spectator? Element data perhaps? A variable? You can simply just add that to the not isPedInVehicle check and make it return nothing when they are a spectator.

I suggested him to put :

local state = getElementData(localPlayer, "player state")
if state == "spectating" then return end

but it still didn't work (we tested).

Edited by koragg
Although it should have worked, always fixed similar problems this way.
Link to comment
52 minutes ago, koragg said:

I suggested him to put :


local state = getElementData(localPlayer, "player state")
if state == "spectating" then return end

but it still didn't work (we tested).

Perhaps it isn't player state? Or perhaps that data is server-side only? Or perhaps player state isn't supposed to be spectating? It should work just fine if you do it like that.

Link to comment
15 hours ago, myonlake said:

Well, how do you define someone a spectator? Element data perhaps? A variable? You can simply just add that to the not isPedInVehicle check and make it return nothing when they are a spectator.

 

Now I have this:

 

client side:

function fuelRefill()
	if not isPedInVehicle(localPlayer) then return end
	local state = triggerServerEvent("getPlayerState", resourceRoot, localPlayer)
	if state ~= "alive" then return end
	for i,marker in ipairs(refill_stations) do
		local p_veh = getPedOccupiedVehicle(localPlayer)
		if isElementWithinMarker(p_veh, marker) and fuel < 100 thenfuel = fuel + r_rate end
	end
end


server side:

function getPlayerState(player)
	local state = getElementData(player, "player state")
	if state then
		return state
	end
	return false
end
addEvent("getPlayerState", true)
addEventHandler("getPlayerState", resourceRoot, getPlayerState)


meta:

<export function="getPlayerState" type="server"/>

Line 3:     local state = triggerServerEvent("getPlayerState", resourceRoot, localPlayer)

Line 3 isn't working yet, how can I make it working?

Link to comment

That's not how event handlers work, you cannot return back information just like that.

You need to make a callback function which is triggered from server-side which returns the state (client refill request -> server says ok -> client refill).

So, for example:

Client-side

function fuelRefill( )
  if ( not isPedInVehicle( localPlayer ) ) then return end
  
  triggerServerEvent( "onFuelRefill", localPlayer )
end

addEvent( "onFuelRefillSuccess", true )
addEventHandler( "onFuelRefillSuccess", root,
  function( )
    local p_veh = getPedOccupiedVehicle( localPlayer )
    
    if ( not isElement( p_veh ) ) or ( not isElement( marker ) ) then return end
    
    for _, marker in ipairs( refill_stations ) do
      if ( isElementWithinMarker( p_veh, marker ) ) and ( fuel < 100 ) then
        fuel = fuel + r_rate
      end
    end
  end
)

Server-side

addEvent( "onFuelRefill", true )
addEventHandler( "onFuelRefill", root,
  function( )
    if ( source ~= client ) or ( not isPedInVehicle( client ) ) then return end
    
    if ( getElementData( client, "player state" ) == "alive" ) then
      triggerClientEvent( "onFuelRefillSuccess", client )
    end
  end
)

Not tested.

However, I would suggest doing what you did originally and just make sure player state is synchronized to the client (fourth parameter of setElementData, it is true by default).

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