Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I ignore a player in a table after touching a part?

So I want to make it so a player gets placed into a table after touching a part, but when I make it like that, the code says it ran over 600 times. So I figured I should just ignore them if they're in a table, how would I do that? When I tried the game crashed... I've tried already, and here's what I've tried.






if hit.Parent:FindFirstChild("Humanoid") then
			hit.Parent.HumanoidRootPart.CFrame = game.Workspace.TpCframe2.CFrame
			local player = game.Players:GetPlayerFromCharacter(hit)
			if player.Name ~= #plrs then
				table.insert(plrs,player)
				print("Added player to plrs table.")
			end
		end






2 answers

local table = {}
if hit.Parent:FindFirstChild("Humanoid") then
    hit.Parent.HumanoidRootPart.CFrame = game.Workspace.TpCframe2.CFrame
    local player = game.Players:GetPlayerFromCharacter(hit)
    if not table[player.Name] then -- detecting if player is not in the table
        table[player.Name] = true -- adding player in table
        print("Added player to table.")
    end
end

You can detect if the player is in the table like this: table.find(plrs, player) with your code. You can easily reform your code to include this.

Just remember you should remove the player when they leave as they can still remain even when destroyed. table.remove(plrs, the player's index) (you can use table.find(plrs, player) to fetch the index again)

2 comments

0

oh wait, so I dont use player.Name?

0

You may use the name or player, indexes do not have a set preference.

This discussion is preserved as it was. New answers, comments, and votes are closed.