script.Parent = nil?

Hi,

I wrote a LocalScript for a glowstick tool to go with a dancing game and I get the following error:

Here is the code:

local RemainingTracks = ReplicatedStorage.Remotes.GetGlowstickHealth:InvokeServer(script.Parent.Name)
local Songs = 0
local EquippedArray = {}

script.Parent.Equipped:Connect(function() -- LINE 18 IS HERE
	EquippedArray[tostring(Songs)] = true
end)

ReplicatedStorage.Remotes.TrackOver.OnClientEvent:Connect(function()
	if EquippedArray[tostring(Songs)] == true then
		RemainingTracks -= 1
		ReplicatedStorage.Remotes.AdjustGlowstickHealth:FireServer(script.Parent.Name)
	end
	
	Songs += 1
end)

if RemainingTracks == nil or RemainingTracks == 0 then
	ReplicatedStorage.Remotes.ConsumableBroken:FireServer(script.Parent.Name)
	script.Parent:Destroy()
	script:Destroy()
end

How can script.Parent be nil with the code still executing??
Any help would be appreciated

Thanks,
Tom :slight_smile:

1 Like

The error is that script.Parent.Equipped = nil not that script.Parent = nil

1 Like

So, according to what line 18 is, this is not the error. Though, what are you trying to do with RemainingTracks -= 1? RemainingTracks is a variable of invoking the server, not a variable for a value. So, what are you trying to do here?

Check out this article: RemoteFunction:InvokeServer (roblox.com)

InvokeServer returns a value, meaning that the variable becomes that value.
It’s important to note that for 7 out of 8 testing players, this script works 100% as intended. Just every now and then this error appears and the person’s glowstick doesn’t work.

I don’t understand what you mean by this. Clearly the output states what the error is, it’s on line 18 that it’s indexing nil with equipped.

As I already said, this would not be the error that is appearing in the output.
Anyway, I’m sorry about that. I didn’t know that. :face_with_hand_over_mouth:

I would try putting the equip connection to the end of the script or adding some wait before it. Cause I think the error is just that the tool isn’t fully loaded and that’s why it says that script.Parent.Equipped doesn’t exist. But I am not sure…

The error says that script.Parent is nil , not Equipped is nil. If equipped was nil it would say that Attempt to Index nil with “:Connect:”

1 Like

No it says that it can’t find any “Equipped” inside script.Parent… That means that it thinks that script.Parent.Equipped doesn’t exist.

Take a look into this : Scripts inside a tool continue to run even after the tool is destroyed?

1 Like

It says attempt to index nil with “Equipped” not nil with script.Parent

The error attempt to index nil occurs when the parent of who’s property we are trying to index is nil. Here we try to index Equipped property from a nil value that is script.Parent.

Read this for more info : Attempt to index nil with - #4 by WallsAreForClimbing

If Equipped isn’t a member of script.Parent it would say that : Equipped is not a valid member of Tool “PathHereToScript”

2 Likes

Sorry for this kind of off-topic post, but:
It’s now that I’m realizing that you thought I meant the error that shows in the output is not the error. I meant the error that I was stating (which apparently isn’t an error), wasn’t the error shown in the output.

Is Script.Parent a tool?

The best is to make a tool variable.

local Tool = script.Parent – Select the tool path here.

That won’t be changing anything.

1 Like

Thank you for your useful reply :laughing:

This is great, but unfortunately it doesn’t fix this error because I’m not destroying the tool at any point other than when the life is 0 and when the life is set to 0 it’s destroyed in the function below. However, the script never runs that condition at the bottom because it errors on line 18 as soon as it runs.

I made a couple of changes on the server and it has reduced the number of times this is happening to about 1 in 16 but the fact that is does still happen sometimes is baffling.

I literally can’t figure out why this is happening :frowning:

Thanks for your help anyway! Really appreciate it :smiley:

1 Like