Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Issue with variables for chat commands?

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by
Elixcore 1337 Moderation Voter
4 years ago

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)
Ad
Log in to vote
0
Answered by 4 years ago

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

Thats it

Log in to vote
0
Answered by
IDKBlox 349 Moderation Voter
4 years ago

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 :)

Answer this question