Problem with connecting and disconnecting

Hello,

I have a problem with connecting and disconnecting, and adding and removing a boolvalue.

I have a lock on script that adds in a boolvalue after you connect it to a humanoid.

Whenever I kill the model with the boolvalue and reconnect to a new model, not only is the boolvalue gone, but the script believes I am disconnecting instead.

I have used tables and numerical indexes to see if my script is wrong or not and I have the same results previously mentioned.

local RS = game:GetService("ReplicatedStorage")
local remote1 = RS.Remotes.AimAtTarget
local remote2 = RS.Remotes.CameraDisconnected

local disconnectEvent
local ancestryChangedEvent
local diedEvent
local bool

remote1.OnServerEvent:Connect(function(plr, newTarget)

	-- Create and set the boolean value
	bool = Instance.new("BoolValue")
	bool.Name = "HomingValue"
	bool.Parent = newTarget
	bool.Value = true

	-- Define the disconnect event
	disconnectEvent = remote2.OnServerEvent:Once(function(plr)
			if bool and bool.Parent then
				bool:Destroy()
				bool = nil
			end
			if disconnectEvent then
				print("disconnected")
				disconnectEvent:Disconnect()
				disconnectEvent = nil
			end
		end)
	end)

Do you guys know how to fix this? As I tried my best to see what was the problem at hand by myself.

You can also put the value in the player instead of the character.

Well how would that work? Wouldn’t it be connected only through the Players device only?

You can use an ObjectValue and put this in the player instead

This just contains a reference to a part.

Yes, I was trying to find reference to part now, but I managed to figure it all out now, but thank you for answering!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.