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

[SOLVED] Can I do an if statement to see if a player's name is in a table?

So I'm trying to make a script that gives people with a tester rank a 2x exp boost in my game. Is there any way I can do this? I tried doing it like it is in the following, but it doesn't work, I tested changing the 10x exp one to a name other than mine, and it just gave me 1x exp.

local testers = {"FoxOwO","S_naZY","jurassicsonic","Knineteen19"}

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function()
		local multi = script:WaitForChild("XP Multiplier")
		local multiplier = multi:Clone()
		if player.Name == "Knineteen19" then
			multiplier.Value = 10
		elseif player.Name == testers[1] or player.Name == testers[2] or player.Name == testers[3] then
			multiplier.Value = 2
		else
			multiplier.Value = 1
		end
		multiplier.Parent = game.Workspace:WaitForChild(player.Name).Humanoid
	end)
end)

1 comment

0

print "table(testers)" possibly?

2 answers

✓ Accepted answer

you can use a generic for loop which literates over a table. You'd use the value to get the object.

--example

local testers = {"FoxOwO","S_naZY","jurassicsonic","Knineteen19"}

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		for i,v in pairs(testers) do -- v is the value and i is the index
			if v == player.Name then
				-- do stuff
				break
			end
		end
	end)
end)

you can also use a numeric for loop,

local testers = {"FoxOwO","S_naZY","jurassicsonic","Knineteen19"}

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		for i = 1,#testers do
			local index = testers[i]

			if index == player.Name then
				--do stuff
				break
			end
		end
	end)
end)

3 comments

0

Oh okay, that answers my question in my comment on the other answer, thanks it worked nicely!

0

I know I should accept this, but at the same time I don't want to ruin your perfect 666 reputation xD.

0

tbh

You are using an if statement incorrectly.

local testers = {"FoxOwO","S_naZY","jurassicsonic","Knineteen19"}
        if player.Name == "Knineteen19" then
            multiplier.Value = 10
	end
        if player.Name == testers[1] or player.Name == testers[2] or player.Name == testers[3] then
            multiplier.Value = 2
        else
            multiplier.Value = 1
        end
        multiplier.Parent = game.Workspace:WaitForChild(player.Name).Humanoid

You could also use a for loop.

4 comments

0

Sorry about indentation, wrote this on chrome.

0

Okay I'm confused about what's wrong with the if statement, but I'll try messing around with a for loop.

0

Mk so I'm trying to use a for i,v in pairs sort of thing, but real quick, what would I use to to gather all of the values in the table?

0

I'm not sure how to put it. You can't use or with 2 totally different arguments.

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