Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Hello My local script breaks after 1 go, i need help please?

The code is in a local script inside a tool and under it is the handle by that i mean the local script and the handle has the same parent, the tool, which is located in the players starter pack. Anyways the issue that it only works once and the only way itll work again is if the player dies, i dont know why and the output doesnt tell me anything please help me

local Tool = script.Parent
player = script.Parent.Parent.Parent
local mouse = player:GetMouse()
debounce = true

Tool.Equipped:Connect(function(mouse)
    print('tool equipped')

	local anima = Instance.new("Animation")
		anima.AnimationId = "rbxassetid://885884797"
			local humanoid = Tool.Parent.Humanoid
				local animationTrack
			wait()
				mouse.Button1Down:Connect(function()
			 if debounce == true then
        	debounce = false
		animationTrack = humanoid:LoadAnimation(anima)
        animationTrack:Play()
        local root = Tool.Parent.HumanoidRootPart
		local rhand = Tool.Parent.RightHand
        root.CFrame = CFrame.new(root.Position,root.Position + Vector3.new(mouse.hit.lookVector.x,0,mouse.hit.lookVector.z))
        wait(0.4)
        local orb = Tool.Handle:clone()
        orb.CFrame = rhand.CFrame
        local bv = Instance.new('BodyVelocity')
        bv.MaxForce = Vector3.new(1e8,110000,1e8)
        bv.Velocity = mouse.hit.lookVector *75
        bv.Parent = orb
        orb.Parent = game.Workspace
        orb.Size = Vector3.new(2,2,2)
			wait(0.2)
		orb.Size = Vector3.new(7,7,7)
        game.Debris:AddItem(orb,3)
        orb.Touched:connect(function(hit)
        local ehum = hit.Parent:findFirstChild("Enemy") or hit.Parent:findFirstChild("Humanoid")
        if ehum and ehum ~= humanoid then
            ehum:TakeDamage(2)
        elseif hit.Anchored == true then
            for i=1,10 do
        local debris = orb:clone()
        debris.Size = Vector3.new(0.5,0.5,05)
        debris.CFrame = orb.CFrame
        debris.ParticleEmitter.LockedToPart = false
        debris.BodyVelocity.Velocity = Vector3.new(math.random(-30,30),math.random(-30,30),math.random(-30,30))
        debris.Parent = game.Workspace
        game.Debris:AddItem(debris,2)
   end
wait(3)
debounce = true
 end
end)
end
  end)

    Tool.Unequipped:Wait()
    if animationTrack then -- if the animation track is not nil then stop the animation
        animationTrack:Stop()
end
end)

1 answer

✓ Accepted answer

There are a number of problems with this code. The first and most important one is that it isn't formatted properly. If it isn't formatted properly, it is much harder to read and very difficult to spot the places where it could go wrong.

I did nothing except run it through a formatting script:

local Tool = script.Parent
Player = script.Parent.Parent.Parent
local Mouse = Player:GetMouse()
Debounce = true

Tool.Equipped:Connect(function(Mouse)
	print('tool equipped')

	local Anima = Instance.new("Animation")
	Anima.AnimationId = "rbxassetid://885884797"
	local Humanoid = Tool.Parent.Humanoid
	local AnimationTrack
	wait()
	Mouse.Button1Down:Connect(function()
		if Debounce == true then
			Debounce = false
			AnimationTrack = Humanoid:LoadAnimation(Anima)
			AnimationTrack:Play()
			local Root = Tool.Parent.HumanoidRootPart
			local Rhand = Tool.Parent.RightHand
			Root.CFrame = CFrame.new(Root.Position, Root.Position + Vector3.new(Mouse.hit.lookVector.x, 0, Mouse.hit.lookVector.z))
			wait(0.4)
			local Orb = Tool.Handle:Clone()
			Orb.CFrame = Rhand.CFrame
			local Bv = Instance.new('BodyVelocity')
			Bv.MaxForce = Vector3.new(1e8, 110000, 1e8)
			Bv.Velocity = Mouse.hit.lookVector * 75
			Bv.Parent = Orb
			Orb.Parent = game.Workspace
			Orb.Size = Vector3.new(2, 2, 2)
			wait(0.2)
			Orb.Size = Vector3.new(7, 7, 7)
			game.Debris:AddItem(Orb, 3)
			Orb.Touched:Connect(function(Hit)
				local Ehum = Hit.Parent:FindFirstChild("Enemy") or Hit.Parent:FindFirstChild("Humanoid")
				if Ehum and Ehum ~= Humanoid then
					Ehum:TakeDamage(2)
				elseif Hit.Anchored == true then
					for i = 1, 10 do
						local Debris = Orb:Clone()
						Debris.Size = Vector3.new(0.5, 0.5, 05)
						Debris.CFrame = Orb.CFrame
						Debris.ParticleEmitter.LockedToPart = false
						Debris.BodyVelocity.Velocity = Vector3.new(math.random(-30, 30), math.random(-30, 30), math.random(-30, 30))
						Debris.Parent = game.Workspace
						game.Debris:AddItem(Debris, 2)
					end
					wait(3)
					Debounce = true
				end
			end)
		end
	end)

	Tool.Unequipped:Wait()
	if AnimationTrack then -- if the animation track is not nil then stop the animation
		AnimationTrack:Stop()
	end
end)

The first and most obvious reason why your code literally will not run a second time is probably because of your Debounce variable. Your code only runs once because the only way it can run again is if under the Touched Connection the Hit.Anchored == true condition is evaluated as true before waiting 3 seconds. That is the only place Debouce is set back to true.

However, as I mentioned, there are many more problems with this code. You have a connection within a connection, for one. It can be done, but beginners commonly make the mistake of not cleaning up connections they generate, which is one of your problems. Every time the Tool is Equipped it would create a new Mouse.Button1Down connection, which would stack if the player Equipped the Tool more than once. Then inside of that connection, you make a Touched connection. It might look like only 3 connections to you, but that is a connection generator if there ever was one.

If you want to keep your connection-ception style, you can, as long as you call the :Disconnect method on connections when you are done with them to clean them up.

Otherwise, you can move them outside of one another, and use global variables to coordinate the script's state.

In addition, you create a new Animation Object each and every time the Equipped event fires. Unnecessary, as you only need one copy, loaded into the Humanoid once. Your other objects may also be unnecessarily generated. If you do need to create a new object each time, it is best to have a template object in the global scope and :Clone() it each time you need a new copy. It is much cleaner, and much more efficient than writing the same properties manually each time.

Remember that each connection starts a new thread. Other than that, good luck!

This discussion is preserved as it was. New answers, comments, and votes are closed.