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.