Script returning a model reference as nil?

so im working on sort of a “tab targeting” system for my game - though at the moment, you just click on targets. in any case, I was in the middle of iterating on it when part of it just bricked itself.

--This script detects when a player clicks this entity and relays it to their tageting handler,
--as well as their local targeting scripts.
local TargetedEntity = script.Parent.Parent
local TargetedEntityHumanoid = TargetedEntity:FindFirstChild("Humanoid")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TargetEvent = ReplicatedStorage.CoreEvents.TargetEvent
local ClickDetector = script.Parent

function TargetEntity (player,TargetedEntity)
	TargetEvent:FireClient(player,TargetedEntity)
	print (TargetedEntity)
end

ClickDetector.MouseClick:Connect(TargetEntity)

its a work in progress so please excuse how garbage it is.

in any case, the problem is that just 10 minutes ago TargetedEntity properly stored the target dummy the click detector is attached to.

image

except now it doesnt. it always returns a nil, and im honestly completely clueless. i didnt change anything in that script since i was working on another one. the first i discovered it was from a print line (now removed) that previously spat out the name of the target, randomly erroring out because you can’t get a name property from nil. it hasnt worked since.

i have no idea what to do to fix this. ive never encountered anything like this. i know chains of .Parent arent exactly best practice, so im certain it definitely has something to do with that abomination. It was the only thing i could think of to use short of giving every entity a unique id, and entering it manually into every instance of this script. so if anyone knows how to fix this, or better yet knows a less wack way of grabbing a model who is the grandparent of the script, PLEASE let me know.

in any case, thank you so much for taking the time to read this post. any help you can provide would be greatly appreciated.

1 Like

Why did you put a second thing there? I’ mnot sure if it’s related, but MouseClick only returns the player who clicked the detector, it’s probably overwriting since it’s in a function and it has the same name as the thing you want to give, remove TargetedEntity on the function scope

--This script detects when a player clicks this entity and relays it to their tageting handler,
--as well as their local targeting scripts.
local TargetedEntity = script.Parent.Parent
local TargetedEntityHumanoid = TargetedEntity:FindFirstChild("Humanoid")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TargetEvent = ReplicatedStorage.CoreEvents.TargetEvent
local ClickDetector = script.Parent

function TargetEntity(player)
	TargetEvent:FireClient(player,TargetedEntity)
	print(TargetedEntity)
end

ClickDetector.MouseClick:Connect(TargetEntity)
3 Likes

well that worked. im not quite so sure how, because it was like that before when it was working, but that made it work. so thank you lel.

2 Likes

Anytime! If you have anymore issues don’t be afraid to make another post!

2 Likes