Does anyone know how to fix this issue with pairs

I’m trying to put a Highlight into every player left alive on a certain team if all their spawns have been destroyed.

The problem is the Highlight won’t even spawn, like it won’t detect the players.

I’m not even really sure how I could go about debugging this and couldn’t find anything on DevForum.

			elseif TeamSpawns.Knights.SpawnsLeft.Value == 0 then 
				if #RemainingKnights ~= 0 then
					
					for _, Knight in pairs(RemainingKnights) do
						
						local Highlight = KnightHighlight:Clone()
						Highlight.Parent = Knight.Character
						delay(0.01, function()
							Highlight.Enabled = true
						end)
						
					end
					
				end
			end	
1 Like

this is “KnightHighlight” btw:

local KnightHighlight = script.KHighlight

i dont think people can help without the whole script?

2 Likes

If “RemainingKnights” is an array you have to use ipairs.

local RemainingKnights = {Player1, Player2, Player3} 

for _, Knight in ipairs(RemainingKnights ) do 
 -- Highlight stuff
end
4 Likes

Highlight instances are capped to around 20 in a game at once, so just a small optimization, put everything you want a highlight on in a model and have the highlights adornee set to that model instead. 2 instances for 10 players is better than 10 for 10.

Anyway, add prints everywhere, see how many times the code is running and what data its reading.

--// Before the first if statements:
print(TeamSpawns.Knights.SpawnsLeft.Value)
--// Before you check how many are left:
print(#RemainingKnights)
--// In the for loop:
print(Knight)
  • If the first one prints the wrong output, you’ve scripted the spawns left system wrong.

  • If the second one only prints once, the check is ONLY happening once and never again.

  • If the second one prints several times but prints 0, you may be indexing your table wrong.
    #table checks the keys in a table, counting up from 0 and stopping when the next index is nil.
    Running this code in the command line shows that: local table1 = {["me"] = 1} local table2 = {[1] = 1, [3] = 2} print(#table1) print(#table2) > Outputs 0, 1


Essentially, tables need to be formatted like this to work:

{
    [1] = Character,
    [2] = Character
}

Not like this:

{
    ["PlayerName"] = Character,
    ["PlayerName"] = Character
}

If you need help reformatting removing keys from tables, this should work with how tables are supposed to be formatted:

table.remove(RemainingKnights, table.find(RemainingKnights, character))

  • If the third one never prints, then RemainingKnights has no entries.

This is really hard for us to debug without the full script, btw.

2 Likes

This is not a direct solution however it might be optimal to do. Instead of creating a highlight for each player, parent all alive players to a model and add one highlight with the adornee set to the model. This will help with the hard cap for the amount of highlights you can have and you only need to enable or disable one highlight instead of adding a new one for each player.

3 Likes

thanks guys i’ll try these out tmrw and see if they work

1 Like

did you solve it?, then mark the reply that helped you with solution

No retroslop please

elseif TeamSpawns.Knights.SpawnsLeft.Value == 0 then 
		for _, Knight in RemainingKnights do
			local Highlight = KnightHighlight:Clone()
			Highlight.Enabled = true
			Highlight.Parent = Knight.Character	
		end
end	

Assuming this isn’t solved - How are you defining RemainingKnights? It’s possible that it isn’t being updated to contain the players.

(Also, the #RemainingKnights ~= 0 is redundant, as pairs({}) will do nothing)

I dont really understand how that line of code is retroslop? deprecated code does not mean retroslop…

Yeah but when it is written nowadays it is retroslop

What do you consider retroslop, cuz I use studs and old meshes and everything in the game is before or in 2016 with everything being on a 0.5 grid. Would you still consider that retroslop?

I’m trying not be like grow a garden and just plaster the texture over everything. I genuinely like the style and I’m trying to do it right. Also thanks a lot for the help.

In this context im talking about code specifically and use of depracated and absolutelly unneeded and illogical features aswell as bloat in it that kills perfomance.

1 Like

You should use task functions cause they are newer, don’t use deprecated functions like wait() delay() and spawn()

1 Like

ohh, no I’m just not the best at scripting


it’s clothing, not roblox functions.
all it took was one search.

Instead of this:

maybe politely notify the OP that the functions they are using are deprecated, and have been replaced by more modern alternatives.

4 Likes

Please ignore the term this other person used for your code! Truthfully, deprecated code is in fact, not “retroslop”, and we try to maintain a respectful environment in the Dev Forum when possible!
Deprecated code is unreliable and has poor compatibility with modern Studio, and that is the real reason you should not use it. Try not to concern yourself with such terms being used against your creations.
Your “retro” style of choice for your game is perfectly fine.
You should prioritize having fun and building something you are proud of, while learning essential information and improving your development abilities!
Godspeed, Fellow Developer!

3 Likes

No you don’t, ipairs is when you want to get both the index and the value of the index. A regular loop would work just fine.

local RemainingKnights = {"Player1", "Player2", "Player3"} 

for _, knight in pairs(RemainingKnights) do 
	print(knight) --> Prints Player1, Player2, Player3
end

I understand I am late to this post, however if I could see the whole code base then I could help further.

1 Like