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

Using _G but not working?

Asked by 8 years ago

So I have 2 scripts and I am trying to use a big global table. For some odd reason it won't work. Console says theres an error on line 12 however the script editor underlines the [] Heres the global table:

_G.Admins =
{
["conman0552"]
}

For some reason it also doesn't like my [] either

My script I want to use it with:

local isAdmin = _G.Admins

function findPlayer(name)
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
        end
    end
end

function onChatted(message, player)
if message == "Close" and isAdmin[player.Name] then --change ":normal" to the name of the command
    game.Lighting.Door.Parent = game.Workspace

    end 
--copy everything from 'if message == ":norma;"' until the first 'end' and paste it between this and the 'end' below this.
end


 --Copy and paste everything from if message == until the first 'end'  and change the building name and command each time.

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(message) onChatted(message, player) end)
end)

and

local isAdmin = _G.Admins

function findPlayer(name)
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
        end
    end
end

function onChatted(message, player)
if message == "Open" and isAdmin[player.Name] then --change ":normal" to the name of the command
    game.Workspace.Door.Parent = game.Lighting

    end 
--copy everything from 'if message == ":norma;"' until the first 'end' and paste it between this and the 'end' below this.
end


 --Copy and paste everything from if message == until the first 'end'  and change the building name and command each time.

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(message) onChatted(message, player) end)
end)

On both the door scripts the error is on line 12

0
Edited, see this in the morning Pyrondon 2089 — 8y
1
I urge you check for the player's ID instead of their name, since players can change their name. XAXA 1569 — 8y

3 answers

Log in to vote
3
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

The reason it's causing an error is because you told the script to check the _G.Admin table in the position of "player.Name". Basically, the table is looking for a key which doesn't exist.

What you should do is do a for loop through the table, and see if the child is an admin, like so:

local isAdmin = _G.Admins

function findPlayer(name)
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
        end
    end
end

function onChatted(message, player)
if message == "Close" then
    for i, v in pairs (isAdmin) do
        if v == player.Name then -- You really should use the IDs like everyone said..
            game.Lighting.Door.Parent = game.Workspace
            break 
        end
    end
end 
--copy everything from 'if message == ":norma;"' until the first 'end' and paste it between this and the 'end' below this.
end


 --Copy and paste everything from if message == until the first 'end'  and change the building name and command each time.

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(message) onChatted(message, player) end)
end)


And you would do the same for the other script, though once again I'm not sure why you don't just put them in the same script. Hope this helped!

EDIT: Also, in your first one, those brackets are not necessary:

_G.Admins =
{
"conman0552",
}

Should work fine.

EDIT 2: Whenever you see this.. where is your global table? You need one LocalScript copy and one Script copy of it. Also,

function findPlayer(name)
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
        end
    end
end

function onChatted(message, player)
    if message == "Close" then
        for i, v in pairs (_G.Admins) do
            if v == player.Name then -- You really should use the IDs like everyone said..
                game.Lighting.Door.Parent = game.Workspace
                break 
            end
        end
    elseif message == "Open" then
        for i, v in pairs (_G.Admins) do
            if v == player.Name then
                game.Workspace.Door.Parent = game.Lighting
                break 
            end
        end
    end 
end




game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(message) onChatted(message, player) end)
end)

0
Well both scripts are on a normal script. Conmmander 479 — 8y
0
Butt Pirate User#11440 120 — 8y
Ad
Log in to vote
1
Answered by 8 years ago

You have a syntax error. You use [] for dictionaries (tables where each index has a value set to it). Example:

local Dictionary = {
    ["A"] = 1,
    ["B"] = 2,
    ["C"] = 3
}
print(Dictionary["B"]) --> 2

For more information on Dictionaries, read this.

For your admin list, you'll have to use a normal table. This means just use string values with names in them.

_G.Admins =
{
    "conman0552"
}
Log in to vote
-2
Answered by
Sirade -2
8 years ago

One cause I could think of is that they both may not be scripts. One may be a normal script, and the other a local script. I'm not sure what the problem is. Have you tried using your "Developer Console" to see if it can indicate an issue?

Here are some links that may help you fix your problem:

http://wiki.roblox.com/index.php?title=Global_function http://wiki.roblox.com/index.php?title=Developer_console

0
Yes I have. And they are all normal scripts not local or any other. I have been using the dev console thats how I know the error with the 2 door scripts is on line 12 and 13 Conmmander 479 — 8y
0
One thing I've noticed you don't have your script count the # of admins. Maybe that could possibly be one of the problems? Sirade -2 — 8y

Answer this question