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

Issue with variables for chat commands?

I'm using a few chat commands in a game I'm producing. I've written out the admins' names correctly, however, whenever I go into test mode in studio it never seems to work. The chat commands only work for the first username after local Admin. Is there any way I can fix this? I don't need help on the actual command, I just need help on why the variables aren't working.

The script is below.

local Admin = 'iiQueenCobra59', 'matthewh1cks', 'LovelyMxddee', 'iimaddieoreo', 'queshtIons', 'DramticRainbowLover'



-- Lighting Command.

game.Players.PlayerAdded:Connect(function(plr) -- Dim Lighting.
if plr.Name == Admin then
	plr.Chatted:Connect(function(msg)
	if msg == "!lightsoff" then
for _,obj in pairs(game.Players:GetChildren()) do
   game.Workspace.MainLighting.SurfaceLight.Brightness = 0.2
end
	end	
		end)
end
end)

Any help would be appreciated.

3 answers

✓ Accepted answer

Hey, glad to answer this kind of question again.

This has been asked many times before, so please try searching instead of posting next time!

Please use a dictionary and access its index for stuff like this. You are setting "Admin" to "iiQueenCobra59" and the rest of the values are set to nil.

local Admin = {
	['iiQueenCobra59'] = true,
	['matthewh1cks'] = true, 
	['LovelyMxddee'] = true, 
	['iimaddieoreo'] = true, 
	['queshtIons'] = true, 
	['DramticRainbowLover'] = true
}



-- Lighting Command.

game.Players.PlayerAdded:Connect(function(plr) -- Dim Lighting.
	if Admin[plr.Name] then
		--code
	end
end)

the variable should be a table like this {'iiQueenCobra59','matthewh1cks','LovelyMxddee','iimaddieoreo','queshtIons','DramticRainbowLover}

Thats it

The reasoning for your issue was because you made Admin = 'iiQueenCobra59', -- Anything passed this Comma was nil

Therefor Making it Admin = {'iiQueenCobra59', } -- Anything passed this Comma would not end up being nil,

when working with things like that, you must make it a table or Dictionary

local Admin = 
{
	'iiQueenCobra59',
	'matthewh1cks',
	'LovelyMxddee',
	'iimaddieoreo',
	'queshtIons',
	'DramticRainbowLover'
}

local function CheckIfPlayerIsAdmin(player)
	for _,admin in pairs (Admin) do
		if player.Name == admin then
			return true
		end
	end
	return false
end

game.Players.PlayerAdded:Connect(function(plr)
	local plrisAdmin = CheckIfPlayerIsAdmin(plr)
	if plrisAdmin then
	    plr.Chatted:Connect(function(msg)
			if msg == "!lightsoff" then -- Dim Lighting.
				for _,obj in pairs(game.Players:GetChildren()) do
					workspace.MainLighting.SurfaceLight.Brightness = 0.2
				end
		    end 
		end)
	else
		print(plr.Name,'is not on the admin list!')
	end
end)

I realize mine was a bit too slow, but Here is this.

Hope it helped you realize your issue :)

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