Jump to content

Nil a value


Fory

Recommended Posts

Hello! I'm trying to figure out what could be wrong, but I can't. Maybe I'm missing a very small detail, but unfortunately it doesn't work.

Turbo system.

[ERROR]: ms_turbo/sourceS.lua:4: attempt to get length of local 'players' (a nil value)

Server:

addEvent("setTurboSound", true)
addEventHandler("setTurboSound", getRootElement(),
	function (vehicle, turboLevel, players)
		if isElement(vehicle) and turboLevel and #players > 0 then
			triggerClientEvent(players, "setTurboSound", vehicle, turboLevel)
		end
	end)

Client:

local turboLevel = getElementData(pedveh, "vehicle.tuning.Turbo") or 0
	if turboLevel >= 4 then
	playSound("files/turbo.mp3")
	triggerServerEvent("setTurboSound", localPlayer, pedveh, turboLevel, getElementsWithinRange(x, y, z, 100, "player"))
end

 

Link to comment

you should check if

Players 

not nil before using it in the trigger call

Server

addEvent("setTurboSound", true)
addEventHandler("setTurboSound", getRootElement(),
	function (vehicle, turboLevel, players)
		if isElement(vehicle) and turboLevel and players and #players > 0 then
			triggerClientEvent(players, "setTurboSound", vehicle, turboLevel)
		end
	end)

Client

local x, y, z = getElementPosition(pedveh)
local players = getElementsWithinRange(x, y, z, 100, "player")
if #players > 0 then
	local turboLevel = getElementData(pedveh, "vehicle.tuning.Turbo") or 0
	if turboLevel >= 4 then
		playSound("files/turbo.mp3")
		triggerServerEvent("setTurboSound", localPlayer, pedveh, turboLevel, players)
	end
end

The change I made in the server code is to add an extra check to make sure that the players variable is not nil before trying to get its length with #players

In the client I added a line to get the current position of the vehicle using getElementPosition this is needed to properly filter the nearby players with getElementsWithinRange I also added an extra check to make sure that there are players found before triggering the server event

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