Attempt To Index Nil with "Value" while using a variable

Hello! I have come across yet another issue with my system!

This time it is in relation to changing a value back to its empty state. Every single time I attempt to change it from “YouJustGotShahBanged” back to just “” it wants to give me Attempt To Index Nil With Value.

This is really frustrating as the code makes sense yet it seems to think that the variable Target does not have a value inside. Screenshots below.

	local wp = game.Workspace:GetChildren()
	local target = nil
	for i, v in pairs(wp) do
		if v:IsA("StringValue") and v.Value == (script.Parent.Parent.Parent.Parent.Parent.Parent.Name) then
			target = v
		end
	end
	
	if target then
		target.Value = nil
	end
	script.Parent.Parent:Destroy()
end)

image

Error message.

Here is the hierarchy.
Thanks for reading and I hope I can find the answer

You can’t use nil on a String.Value, use “” instead

No, it’s implying that the object that you are trying to find the prop Value of is NULL

The value stays the same even after closing the GUI which is not what I require. I need it to reset back to its unused state after closing.

Here is the problem

It will check if the child has a property called value. I think it would work if you put the value check under the IsA()

Edit: Something like

for i, v in pairs(wp) do
	if v:IsA("StringValue") then
		if v.Value == script.Parent.Parent.Parent.Parent.Parent.Parent.Name then
			target = v
		end
	end
end

How do I check if it has that property?

Do I just do :IsA(“Value”)?

No, when you do that if statement, it checks both at the same time. So since that the child does not have a property called value, it will return an error. So you should check if it is a string value, before checking the value of the string

No it’s not. He’s trying to change the string value to nil and that will cause an error

	local wp = game.Workspace:GetChildren()
	local target = nil
	for i, v in pairs(wp) do
		if v:IsA("StringValue") then
			if v.Value == script.Parent.Parent.Parent.Parent.Parent.Parent.Name then
				target = v
			end
		end
	end
	
	target.Value = ""
	print(target)
	
	script.Parent.Parent:Destroy()
end)

Nope it still says that the target.value = “” is wrong unfortunately.

Try changing your condition to the following:

if target ~= nil then
    target.Value = nil
end

If this does not help, could you please specify which line the error occurs in exactly.

Can you click on the error and specify which line?

And is the script the one you showed in the picture?

Edit: Oh yeah, and you should not use Name to name an instance, it will cause some bad errors.

image

So, there are no errors anymore, however the value does not reset back to “” anymore.

Edit: It didn’t reset in the first place, it just won’t tell me there’s an error anymore.

Try naming the value to something else. If you use Name to fimd the string value, you would get the parent of the string value

1 Like

You shouldn’t call a variable Name. Change it to PlrName or something. Name is a property of the variable and will cause you issues

Have renamed it to “Person” and can confirm that it has made no difference.

Reverted the script back to original and it tells me the error is on Line 12 (target.Value = "").

Edit: This is the code, I only took away the if target ~= nil.

	local wp = game.Workspace:GetChildren()
	local target = nil
	for i, v in pairs(wp) do
		if v:IsA("StringValue") then
			if v.Value == script.Parent.Parent.Parent.Parent.Parent.Parent.Name then
				target = v
			end
		end
	end
	
	target.Value = ""
	
	script.Parent.Parent:Destroy()
end)

Attemp to index nil with "Value", what’s nil in this case?

RGui is short for “RetrieveGui”. This is for closing the gui. Once closed it will remove all the variables that state who is using the gui and puts it back into its unused state so that the next person can use it and their name can be stored.

This means that in the in pairs loop, the value of target is not being set. You can then conclude that one of the conditionals in the in pairs loop is never true and thus the value of target is never set to v. Please recheck all your conditions and make sure you’re not making any mistakes. As @TwyPlasma and @ZombieCrunchUK suggested, consider changing the ValueObject’s name to something other than Name. You should generally avoid changing instance’s name to Name as the script thinks you’re referencing the Name of the instance you previously referenced. Could you please elaborate on what this returns: script.Parent.Parent.Parent.Parent.Parent.Parent.Name.

I tested your code and it works fine here. The only things I changed was the value test and the bit at the bottom which destroys a script.

local wp = game.Workspace:GetChildren()
	local target = nil
	for i, v in pairs(wp) do
		if v:IsA("StringValue") then
			if v.Value == "hello" then
				target = v
			end
		end
	end
	
	target.Value = ""
	print(target)

Worked as expected

script.Parent.Parent.Parent.Parent.Parent.Parent.Name .

This returns the player who clicked the close button.