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

How do I check if a player name isn't in a table? [SOLVED]

Here's the script:

local cooldown = false
	if hit.Parent:FindFirstChild("Humanoid") then
		for i,v in pairs (touchedplayers) do
		if not hit.Parent.Name == v and not cooldown then
		cooldown = true
		table.insert(touchedplayers,hit.Parent.Name)
		game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui.TextLoader.UserText.Visible = true
		loop()
		   end
		end
	end
end)

I want to check if the player that touched the brick isn't in a table. If it is then it would do nothing but if it's not It would add him to it and call a function. When I go ingame nothing happens when I touch it, and there isn't anything in the output.

1 answer

✓ Accepted answer
local touchedplayers = {}

function CheckPlayerList(PlayerName,List) 	--This checks is a player is in the specified list
	for _,v in pairs(List) do				--This goes through the list
		if v == PlayerName then			--This checks if the list has the players name in
			print(PlayerName .. ' is already in the list')
			return true				--This returns true if the player is in the list
		end
	end
end

local cooldown = false
script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
		if CheckPlayerList(hit.Parent.Name,touchedplayers) or cooldown then return end	--This line will stop the script going further if the players name is already in the list or if cooldown is true
        cooldown = true
        table.insert(touchedplayers,hit.Parent.Name)
		print(hit.Parent.Name .. ' successfully added to the list')
        game.Players[hit.Parent.Name].PlayerGui.TextLoader.UserText.Visible = true
        loop()
    end
end)

From what I see, your issue is "for i,v in pairs (touchedplayers) do", which is trying to go through a list which has nothing in it yet, so the part of the script after it won't run unless something's added to the list. There was no output shown as the script runs fine, it was your method at fault. Hope this helps

1 comment

0

It worked! Thank you so much for your help! :D

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