Helo loop, I'm getting back into gLua.

Hello, I’ve recently started getting myself back into gLua and I’ve started on working on the tables. I’m trying to make a little script what functions when a player is in the table “playerCount” he will be ignited for 30 seconds then moved to another table where he won’t be harmed again.

Upon this it seems when i loop if the table has the value ( if the player is in the ‘playerName’ table ) he shouldn’t be ignited when really for some reason even though he’s in the playerName table he still gets burned, I can’t figure this out.

The way this should function is when the player is the 1st index of the playerCount table he will be burnt, and after he’s removed from that table and added to playerName.

EDIT: Also, even when I’m in playerName table I’m still being ignited when I shouldn’t be.


function playerData(plr, att, dmg)


local	playerCount = {}
local playerName = {}

for k,v in pairs(player.GetAll()) do 

	if table.HasValue( playerName, v) then
		print(v.."Is sorted in the playerName table.")
	 else
		table.insert(playerCount, v)
	end
	

	if playerCount[1] then
		if v:IsValid(entity) then
		  v:Ignite( 30 )
		  table.remove( playerCount, 1)
		 table.insert( playerName, v)
	 	end

	end

end

print("PLAYERS")
PrintTable( playerCount )
print("PLAYER SAFE")
PrintTable( playerName )



end
--timer.Simple(4, playerData)

hook.Add("PlayerSpawn", "table_move_1", playerData)

Thanks for your time, I hope you can assist me :slight_smile:

I think the problem may be with this:



if playerCount[1] then
	if v:IsValid(entity) then
		v:Ignite( 30 )
		table.remove( playerCount, 1)
		table.insert( playerName, v)
	end
end


You’re checking if playerCount[1] exists, but then you’re doing v:Ignite (rather than igniting the playerCount key), with v meaning whatever the current player is in your loop. Maybe try igniting the key instead:



playerCount[1]:Ignite( 30 )
table.remove(playerCount, 1)
table.insert(playerName, playerCount[1])


Not quite sure if that will help or not since my brain isn’t working today, but if it doesn’t, you could also try creating your tables OUTSIDE your function. Both your tables are being locally created INSIDE the function, meaning once the function ends, the tables won’t exist. Not sure if you wanted this to happen or not, but just thought I may as well say it here.

[LUA]

local playerCount = {};
local playerName = {};

function playerData(ply)

if (table.HasValue(playerName, ply)) then
	print(ply:Nick() .." is stored in the playerName table.")
else // if he isnt stored in the "safe table"
	table.insert(playerCount, ply) // insert him into the "ignition" table
	table.remove(playerName, ply) // remove him from the "safe" table
end
	

if (table.HasValue(playerCount, ply)) then // if he exists in the "ignition" table
		ply:Ignite(30)
		table.remove(playerCount, ply) // remove from ignition table
		table.insert(playerName, ply) // insert into safe table
	end

print("PLAYERS")
PrintTable( playerCount )
print("PLAYER SAFE")
PrintTable( playerName )

end
–timer.Simple(4, playerData)

hook.Add(“PlayerSpawn”, “table_move_1”, playerData)

[/LUA]

Don’t use a for loop with the player spawn hook because then the ignition will happen anytime that ANY player spawns. For example, if you were in the table, but not supposed to be ignited until your next respawn, then anytime that someone spawns you would be ignited. Also, store your tables outside of your method so that they don’t get overwritten each time a player spawns. Also, with the way that my code is currently set up above, it is basically an endless loop. Each time the player spawns he will be found in the “safe” table, moved to the “ignite” table, ignited, then moved back to the “safe” table. I’m sure that this isn’t exactly how you want it to work, but I hope you can figure the rest out! Good luck!

Yes, I figured that out the hardest way, it was common sense to use ‘local’ because the was i was approaching it wasn’t right, every time i executed the function everything was just reassigning and nothing was changed.

Thank you for the assistance guys.