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

'Attempt to index a nil value' but nothing is nil?

Asked by 5 years ago
Edited 5 years ago

I'm working on a system that runs a RemoteEvent when a gui button is pushed.

Here we have a simplified version of my script that should provide all non functioning details.

local rem = "!armspurchase"
local user = game.Players.LocalPlayer
local Armors = {"GoldArmor", "SteelArmor"}

for i = 1, #Armors do
    local Pass = user.PlayerGui.ScreenGui:findFirstChild(Armors[i].Name).TextButton
    Pass.MouseButton1Down:Connect(function()
        game:GetService("ReplicatedStorage"):FindFirstChild(rem):FireServer(user.Name, Armors[i].Name)
    end)
end

For some reason, even though everything is accounted for, it tells me that I'm trying to index a nil value on line 8???

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago

You created an array holding Armour Types. I assume these Strings are relative to the Names of each Child iterated in ReplicatedStorage. The answer to your problem is simple, you tried to use .Name on a String inside a table. The table’s element isn’t an Object defined with this property therefore it would be a nil property. Simply enough it would still translate to the Armour Type without .Name so just remove that, and you’re good to go!

0
Don’t forget to accept this answer if it works out:3 Ziffixture 6913 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Try to index the remote with a local. this can be:

local rem = game:GetService("ReplicatedStorage"):FindFirstChild("RemoteName")

after this you just need to fire the remote.

local rem = game:GetService("ReplicatedStorage"):FindFirstChild("RemoteName")
local Armors = {"GoldArmor", "SteelArmor"}
local Plr = game.Players.LocalPlayer

for i,v in pairs(Armors) do
    v.MouseButton1Click:Connect(function()
        rem:FireServer(Plr[v])
    end)
end

Test that out

0
Also in your fire server you dont need to use .Name Heck you could have probably just sent v as the argument for fire server Protogen_Dev 268 — 5y

Answer this question