Scripts breaks after disabling and re-enabling it

Hello fellow developers,
I got a problem in my script and I do not know how I could fix this issue.


So here, i’m trying to disable my code and re-enable it, but the script completely breaks when doing that.

AttackCode.Disabled = true
wait(1)
AttackCode.Disabled = false

Anyone got an idea of why it does that?

Can you show us your code inside AttackCode?

3 Likes

My attack code is actually 200 lines, but if you really want it, there it is.

script.Parent.OnServerEvent:Connect(function(plr) -- The attack is fired by a remote.

		local Character = plr.Character
		local Attack_Clone = Instance.new("Part")
		Attack_Clone.Transparency = 1
		Attack_Clone.Anchored = true
		Attack_Clone.CanCollide = false
		Attack_Clone.Size = Vector3.new(5,5,5)
		local DamageBillboard = RS:WaitForChild("DamageBillboard"):Clone()

	 local function Damage(hit)

		local char = hit.Parent
		local target = char:FindFirstChildOfClass("Humanoid")
		local IsMob = char:FindFirstChild("IsMob")
		local Blood = game.ReplicatedStorage.Blood:WaitForChild("Attachment")
		local Dealer = game.Players:GetPlayerFromCharacter(Tool.Parent)

		
		 local function TagHumanoid(humanoid, dealer)
			local Creator_Tag = Instance.new("ObjectValue")
			Creator_Tag.Name = "creator"
			Creator_Tag.Value = dealer
			game.Debris:AddItem(Creator_Tag, 2)
			Creator_Tag.Parent = humanoid
		end

		local function SoulEffect()
			local SoulEffectBeam = RS:WaitForChild("SoulEffect"):Clone()
			SoulEffectBeam.Parent = workspace
			local Attachment0 = Instance.new("Attachment", char:WaitForChild("Head"))
			local Attachment1 = Instance.new("Attachment", Tool.Handle.Sword)
			SoulEffectBeam.Attachment0 = Attachment0
			SoulEffectBeam.Attachment1 = Attachment1
			wait(2.5)
			SoulEffectBeam:Destroy()
		end


		if char.Name == Character.Name then return end
		local IsBlocking = char:WaitForChild("IsBlocking").Value



		if hit and target then


			-- Mob not blocking
			if IsMob and DamageDebounce == false and IsBlocking == false then

				local blood = Blood:Clone()
				local sound = script:WaitForChild("HitSound"):Clone()

				blood.Parent = hit.Parent:WaitForChild("HumanoidRootPart")
				game.Debris:AddItem(blood, BloodDuration)
				sound.Parent = hit.Parent

				DamageDebounce = true

				sound:Play()
				target:TakeDamage(DefaultDamage)
				TagHumanoid(target, Dealer)
				target.Died:Connect(SoulEffect)

				DamageBillboard.Parent = hit.Parent:WaitForChild("HumanoidRootPart")
				DamageBillboard.TextLabel.Text = "-"..tostring(DefaultDamage)
				TS:Create(DamageBillboard, DamageBillboardTI, {SizeOffset = Vector2.new(0,1)}):Play()

				wait(DamageCooldown)

				sound:Stop()
				sound:Destroy()

				DamageDebounce = false

			elseif IsMob and DamageDebounce == false and IsBlocking == true then

				local sound = script:WaitForChild("HitSound"):Clone()

				DamageDebounce = true

				sound.Parent = hit.Parent

				target:TakeDamage(DefaultDamage / 2)
				TagHumanoid(target, Dealer)
				sound:Play()
				target.Died:Connect(SoulEffect)

				DamageBillboard.Parent = hit.Parent:WaitForChild("HumanoidRootPart")
				DamageBillboard.TextLabel.Text = "-"..tostring(DefaultDamage)
				TS:Create(DamageBillboard, DamageBillboardTI, {SizeOffset = Vector2.new(0,1)}):Play()

				wait(DamageCooldown)

				sound:Stop()
				sound:Destroy()

				DamageDebounce = false

		elseif IsMob == nil and DamageDebounce == false and IsBlocking == false then

				local blood = Blood:Clone()
				local sound = script:WaitForChild("HitSound"):Clone()

				DamageDebounce = true

				blood.Parent = hit.Parent:WaitForChild("HumanoidRootPart")
				game.Debris:AddItem(blood, BloodDuration)

				sound.Parent = hit.Parent

				sound:Play()
				target:TakeDamage(DefaultDamage * (plr.Attributes.Attack.Value + 1))
				TagHumanoid(target, Dealer)
				target.Died:Connect(SoulEffect)

				DamageBillboard.Parent = hit.Parent:WaitForChild("HumanoidRootPart")
				DamageBillboard.TextLabel.Text = "-"..tostring(DefaultDamage)
				TS:Create(DamageBillboard, DamageBillboardTI, {SizeOffset = Vector2.new(0,1)}):Play()

				wait(DamageCooldown)

				sound:Stop()
				sound:Destroy()

				DamageDebounce = false

		elseif IsMob == nil and DamageDebounce == false and IsBlocking == true then

				local sound = script:WaitForChild("HitSound"):Clone()

				DamageDebounce = true
				target:TakeDamage(DefaultDamage * (plr.Attributes.Attack.Value + 1))
				TagHumanoid(target, Dealer)
				target.Died:Connect(SoulEffect)

				sound.Parent = hit.Parent

				sound:Play()

				DamageBillboard.Parent = hit.Parent:WaitForChild("HumanoidRootPart")
				DamageBillboard.TextLabel.Text = "-"..tostring(DefaultDamage)
				TS:Create(DamageBillboard, DamageBillboardTI, {SizeOffset = Vector2.new(0,1)}):Play()

				wait(DamageCooldown)

				sound:Stop()
				sound:Destroy()

				DamageDebounce = false

			end
		end
	end
		
		local ID = "rbxassetid://8960226474"
		
		local Animation = Instance.new("Animation")
		Animation.AnimationId = ID
		local Animation_Loader = Character.Humanoid:LoadAnimation(Animation)
				
		-- Main Code
	
		Animation_Loader:Play()
		Animation_Loader:AdjustSpeed(1)

		Attack_Clone.Parent = workspace
		
		game.Debris:AddItem(Attack_Clone, AttackDuration)
		
		Attack_Clone.Touched:Connect(Damage)

		Attack_Clone.CFrame = Character.HumanoidRootPart.CFrame + Character.HumanoidRootPart.CFrame.LookVector * StartDistanceFromCharacter

		Character.Humanoid.WalkSpeed = 0

		script.Parent.Parent.Parent.Handle.Sword.Trail.Enabled = true
		script:WaitForChild("Sound"):Play()

		wait(Time)

		script.Parent.Parent.Parent.Handle.Sword.Trail.Enabled = false
		Character.Humanoid.WalkSpeed = DefaultSpeed + plr.Attributes.Speed.Value

end)

What does the output say about the error, if any? Can you reply with the error?

1 Like

There are no errors in the Output. I also saw in the explorer that the script IS actually disabling and re-enabling. Meaning that my code is working. But even while being enabled, it is not working.

Enabling and disabling scripts isn’t recommended, because this happens. Rather have some signal within that indicates that the thing is being turned off. Something like this:

Signal:Fire('DisableScript')

And in the listener:

Signal:Connect(function()
    ... -- here disable anything you need to disable
end)

The script would obviously break after doing such operation like disabling and enabling.

1 Like

Could you tell me where should I do these functions?

Well then, perhaps it is this?

blood.Parent = hit.Parent:WaitForChild("HumanoidRootPart")

You probably need to use a FindFirstChild() function too:

blood.Parent = hit.Parent:FindFirstChild("HumanoidRootPart") or hit.Parent:WaitForChild("HumanoidRootPart")

Once HumanoidRootPart exists, WaitForChild will instead look for another instance with the same name, even if it exists already. Check if it exists before waiting for it.

1 Like

The error isn’t in the attack code. I don’t know if you read my post but the script completely breaks whenever disabling and re-enabling it.

It can possibly be in the Attack code, even if it seems that it was caused by the disabler script.

To check add print statements in the Attack code first if it actually responds. Like so:

local function A()
	print("Func A is running")
	-- all your code stuff
	print("Func A finished")
end

if <statement> then
	print("aihfahufd")
	-- your code
	print("yay")
end

-- etc...

If both starting and ending print()s worked, there’s no problem. If the ending statement did not print, the error lies within those starting and ending print()s.

I tried it. It printed. Which means the script is running, right?

If the Attack script is printing, even after reenabling it, the problem must lie within your Attack code then. The WaitForChild functions might really be the problem.