My script wont work

I’m trying to achieve where if a player clicks a part and they are one of the listed people they change teams.

I cannot figure out how to make the list of people work.

I’ve tried using specific variables for every player, but I’m trying to make it simpler and less likely to not work.

Any help is appreciated.
I tried, I’m new to scripting and this in general but I’m hoping I can learn from this.
I changed the player variables to PlayerName just for privacy reasons.
The code:

local allowedPlayers = {
	"PlayerName",
	"PlayerName"
}

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	print(player.Name .. " changed to PD team.")
	if player.Name == allowedPlayers then
		print(player.Name .. " has successfully changed to PD team.")
		player.Team = game.Teams["Climont PD"] 
	end
end)

You’re basically saying if the player name is the same as the table

2 Likes

That allowedPlayers variable is not allowed player’s name. It’s an array including allowed players’ name.

So you can use this. (It checks if the player’s name is included in the array.)

if table.find(allowedPlayers, player.Name) then
– Code here
end

Also as ppl can change their username, I recommend you to use UserId instead of Username because id never changes. You can find the id in player’s web profile url.

2 Likes
local allowedPlayers = {
	"PlayerName",
	"PlayerName"
}

script.Parent.ClickDetector.MouseClick:Connect(function(player)
	print(player.Name .. " has attempted a change to PD team.")
	if table.find(allowedPlayers, player.Name) then
		print(player.Name .. " has successfully changed to PD team.")
		player.Team = game.Teams["Climont PD"] 
        player:LoadCharacter() - Resetting them
	end
end)

However, I recommend using your group or userids instead of names.
As a county roleplay game developer, it is always nice to see new games.
Good luck within your game!

1 Like