Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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!

1 comment

0

Don’t forget to accept this answer if it works out:3

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

1 comment

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

This discussion is preserved as it was. New answers, comments, and votes are closed.