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

attempt to index a nil value?

Asked by 6 years ago

Hi guys, so, I've spent 2 hours (It's 4:30AM now) trying to solve ONE simple error. I've made this script that would give certain users a weapon and a GUI above their head.

The script is pasted in StarterPack (It's a local script), where a BillboardGui is the script's child, and the Textlabel is the Gui's Child.

Please keep in mind I'm a literally all-new scripter and don't know much about usage of variables and such, but this is what I came up with:

local plr = game.Players.LocalPlayer
local name = plr.Name
local admins = Instance.new("BoolValue",game.Workspace.Admins)
local tool = game.ServerStorage['weapon']
local gui = script.Role


if game.Workspace.Admins:FindFirstChild(name) == nil then
admins.Name = name
admins.Value = false
    else
        print("BoolValue already exists!")
end

if name == "NameHere" or name == "NameHere" then
    admins.Value = true
    print(plr.Name.." is an Admin!")
    tool:Clone().Parent = plr.Backpack
    gui.Role.Visible = true
    gui:Clone().Parent = game.Workspace:FindFirstChild(name).Head
    gui.Role.Visible = false
    print ("Added GUI and Tools!")
else
    print(plr.Name.." is not an Admin!")
end

Now, the error I'm receiving is; "Players.XaqiZono.Backpack.Administrators:20: attempt to index a nil value" The error is occuring on line 20, the part where I clone the GUI inside the script, and move the cloned GUI onto the user's head. How can I fix this?

Also; How do I stop my script from executing UNTIL LocalPlayer is loaded?

And, one more question; I don't really know the diffrence between Local script and Server Script, but is there a diffrent way of calling a LocalPlayer via a Server Script?

Thanks!

1 answer

Log in to vote
0
Answered by 6 years ago

I don’t see why you have a if name == “NameHere” or name == “NameHere” because you are just checking for the same thing twice. Also instead of looking in the workspace for your own character, use “player.Character”.

I recommend you to read more about Filtering Enabled, because if you have it enabled, then this Gui will only be visible/available to that player/client. You are trying to access the ServerStorage from a client, which is not possible as well, it might be possible in Studio, but not in a “real online game”.

You are also using some deprecated methods I’d really recommend stop doing now. Like

Instance.new(Name, Parent)

Rather do

local newItem = Instance.new(Name) newItem.Parent = Parent

Just keep this server-sided .

0
Thanks! I've implemented the new Instance method, and made a Server-sided script rather than a local one. Used a function to address the player.Character and it all works now! User#20989 0 — 6y
0
No problems! 1TheNoobestNoob 717 — 6y
Ad

Answer this question