Hi, I was trying to make a damaging ball of magic that follows players and I encountered this issue: in the following script, connection:Disconnect() does not work.
connection = game:GetService("RunService").Heartbeat:Connect(function()
Target = workspace:FindFirstChild("Enemy")
if not Target then
warn("No target")
Ball:Destroy()
for i, v in pairs(game.Workspace:GetDescendants()) do
if v:FindFirstChild("Creator") then v:Destroy() end
end
connection:Disconnect()
end
(Also please avoiding workspace:GetDescendants() if possible. If you have a particularly large map this will cause large lagspikes. Consider using CollectionService)
Maybe this is why, it could be perhaps the if statement isn’t running, but if it is. It could be because of that for loop, as the script will wait for the for loop to be finished and THEN disconnect it so maybe try putting the event:Disconnect() above the for loop.
local BaseBall = game.ReplicatedStorage:WaitForChild("Ball")
local Event = game.ReplicatedStorage:WaitForChild("SendMagic")
local Damage = 25
local MaxDuration = 10
local Speed = 1
local Accelerartion = 1.1
Event.OnServerEvent:Connect(function(player)
local Ball = BaseBall:Clone()
Ball.Parent = workspace
Ball.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position + (player.Character.HumanoidRootPart.CFrame.LookVector*2), player.Character.HumanoidRootPart.Position)
Ball.Size = Vector3.new(0.1,0.1,0.1)
Ball.Anchored = true
local Tag = Instance.new("StringValue")
Tag.Name = "Creator"
Tag.Value = player.Name
Tag.Parent = Ball
local HasDamaged = Instance.new("BoolValue")
HasDamaged.Name = "HasDamaged"
HasDamaged.Value = false
HasDamaged.Parent = Ball
HasDamaged:GetPropertyChangedSignal("Value"):Connect(function()
if HasDamaged.Value == true then
if Ball then Ball:Destroy() end
end
end)
wait(0.5)
Ball.Anchored = false
local CurrentSpeed = Speed
connection = game:GetService("RunService").Heartbeat:Connect(function()
Target = workspace:FindFirstChild("Enemy")
if not Ball then
warn("No ball")
connection:Disconnect()
end
if not Target then
warn("No target")
Ball:Destroy()
connection:Disconnect()
for i, v in pairs(game.Workspace:GetDescendants()) do
if v:FindFirstChild("Creator") then v:Destroy() end
end
end
if HasDamaged == true then
warn("HasDamaged")
Ball:Destroy()
connection:Disconnect()
end
if (Target.HumanoidRootPart.Position - Ball.Position).Magnitude <= 1 then
warn("Magnitude <= 1")
Ball:Destroy()
Target.Humanoid:TakeDamage(Damage)
connection:Disconnect()
end
local Direction = (Target.HumanoidRootPart.Position - Ball.Position).unit
CurrentSpeed = CurrentSpeed + Accelerartion
Ball.Velocity = Direction * CurrentSpeed
end)
local debounce = false
Ball.Touched:Connect(function(hit)
if debounce or Ball.HasDamaged.Value == true then return else debounce = true end
if not Target then Ball:Destroy() return end
if hit:FindFirstAncestor(Target) then
Ball.HasDamaged = true
workspace.Enemy.Humanoid:TakeDamage(Damage)
end
debounce = false
end)
spawn(function()
wait(MaxDuration)
if Ball then Ball:Destroy() end
connection:Disconnect()
end)
Could you add a print statement under this line Event.OnServerEvent:Connect(function(player) to see if this is running before the connection is disconnected?
I’m still homing in on the possibility of connection being redefined before your code tries to disconnect it.
I’m not seeing a local connection = nil before you define connection, which means it’s overriding an existing connection variable. Try defining the variable as nil right before you assign it.