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

Error: "Username is not a valid member of Players" in my admin commands script?

I'm trying to make a script where the player says :open to open the door, but I'm getting the error "h6rmless is not a valid member of Players."

Here is the script:

local Admins = {"h6rmless"}
local Prefix = ":"

for i,v in pairs(Admins) do
	game.Players(Admins[i]).Chatted:connect(function(msg)
		if msg:lower() == Prefix.. "open" then
		game.Workspace.Door.CanCollide = false
		end
	end)
end

2 comments

0

im gusseing the problem is coming from line 05, and its probarly not working since "h6rmless" isnt a child of Players yet, youre searching for a player named "h6rmless" and i guess he doesnt exist

0

You're calling Players as if it were a function, use [Admins[i]] for whenever you need to search for a specific part. This isn't good practice to set a .Chatted event for a player that may or may not be in the game. Instead, set a .Chatted event for every player that joins the game, or, set a .Chatted event for every admin that joins the game.

1 answer

✓ Accepted answer

It is not a valid member of players because 'h6rmless' may not exist in Players. You need to check if the player is an admin inside the PlayerAdded or Chatted event, not outside of it.

local PlayerService = game:GetService("Players")

--Set admins table as a dictionary.
local Admins = {["h6rmless"] = true}
local Prefix = ":"


PlayerService.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(msg)
		--Check if the chatter is an admin.
		if Admins[player.Name] == true then
			--Check for the message chatted, do everything else here.
			if msg:lower() == Prefix.."open" then
				workspace.Door.CanCollide = false
			end
		end
	end)
end)

or you can do this:

PlayerService.PlayerAdded:Connect(function(player)
	if Admins[player.Name] == true then
		player.Chatted:Connect(function(msg)
			if msg:lower() == Prefix.."open" then
				workspace.Door.CanCollide = false
			end
		end)
	end
end)

1 comment

0

Thank you SO much! This really helps me a lot. Thanks so much for helping me with this.

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