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

Why is my simple admin checking script with a bindable function not working?

Asked by 6 years ago

So this is a simple script where a player steps on a brick and it checks to see if it is an admin or not. If the player is not an admin, they die. If the player is, the brick turns yellow. I wrote the script corresponding to a scripting tutorial: https://www.youtube.com/watch?v=jQ0iClyt_zA&t=312s

Here is the script with the bindable function and list of admins:

admins = {"crystalclay", "Player1", "Player2", "John", "Bob"}

script.GetList.OnInvoke = function(name)
    isAdmin = false
    for i,v in pairs (admins) do
        if name == v then 
            isAdmin = true
            break
        end
    end
    if isAdmin == true then 
        return true
    else
        return false
    end
end

The problem with this script is that, on line 4, the first "isAdmin" is underlined in blue.

Here is the script for the part:

script.Parent.Touched:connect(function(hit)
     humanoid = hit.Parent:FindFirstChild("Humanoid") -- The first "humanoid" is underlined in blue
    if humanoid then 
         playerName = hit.Parent.Name -- "playerName" is underlined in blue
        isAdmin = game.Workspace.ListoAdmins.GetList:Invoke(playerName) -- "isAdmin" is underlined in blue
        if isAdmin == true then 
            Part.BrickColor = BrickColor.new("Bright Yellow") --"Part" is underlined in blue
        else
            humanoid.Health = 0

        end
    end
end)

I do not understand why there are blue underlines as I double checked and typed the script exactly as Peaspod did in this video.

Here is the error message I'm getting: 21:51:31.537 - Workspace.Part.Script:7: attempt to index global 'Part' (a nil value)

0
EDIT: THE SCRIPT WORKS, AFTER I CHANGED PART TO SCRIPT.PARENT. The script worked how it should, but the part turned gray instead of yellow. And the blue underlines are still there. Otherwise, it works fine! However, it would be great if somebody could explain to me why this happened so. crystalclay 70 — 6y
1
for that first isAdmin, change it to: local isAdmin = false PolyyDev 214 — 6y

1 answer

Log in to vote
1
Answered by
XAXA 1569 Moderation Voter
6 years ago

Since you wanted an explanation, Part was underlined in blue because it wasn't defined anywhere in the script. If you had added local Part = script.Parent at the beginning of your second script, it would have worked.

As for your other blue underlines, this typically happens because it defined globally instead of locally (eg. isAdmin = blah instead of local isAdmin = blah). It is recommended to define variables locally wherever and whenever possible because a) it takes less time to access local variables, and b) global variables are shared between different "scopes". For example:

function a()
    g = 2
end

function b()
    print(g)
end

a()
b() -- prints 2, even though g wasn't defined anywhere inside b().

function c()
    local l = 2
end

function d()
    print(l)
end

c()
d() -- prints nil, because l wasn't defined in d()'s scope. This is clear.
0
Ooh, that makes sense. Thank you for your help! Just one question, If mine was underlined, then why wasn't Peaspod's? Was it because it was 2013? crystalclay 70 — 6y
1
Yes, it is. Intellisense was new back then. XAXA 1569 — 6y
Ad

Answer this question