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

Tool breaks when touching a part?

So, i have an issue when my sword is swung and hits a part it breaks, i was not able to find a solution to this i tried to add an else statement but it still broke the whole thing. If anyone could help thanks.

Here is my code:



local player = game.Players.LocalPlayer

player.CharacterAdded:Connect(function()

local debounce = false
local hit = false
local tool = script.Parent
local Animations = tool:WaitForChild("Animations")
local min = tool.Min
local max = tool.Max

local animation1 = Animations:FindFirstChild("SwordIdle")
local animation2 = Animations:FindFirstChild("SwordSwing")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local holdanim = humanoid:LoadAnimation(animation1)
local swinganim = humanoid:LoadAnimation(animation2)


local function Equipped()
	
		holdanim:Play()
end

local function Unequipped()
	
		holdanim:Stop()
end

local function Swing()
	
if not hit then 
	
	hit = true
	holdanim:Stop()
	swinganim:Play()
	wait(1.3)
	holdanim:Play()
	hit = false
	end
end

local function SwordCore(hit)
	if not debounce then
		
		debounce = true
			wait(0.2)
			
	local enemy = hit.Parent:FindFirstChild("Humanoid")
		if enemy then
			enemy.Health = enemy.Health - (math.random(min.Value, max.Value))
					wait(1.3)
					debounce = false
		end
	
		end
end


tool.Equipped:Connect(Equipped)
tool.Unequipped:Connect(Unequipped)
tool.Activated:Connect(Swing)


local player = game.Players.LocalPlayer

player.CharacterAdded:Connect(function()

local debounce = false
local hit = false
local tool = script.Parent
local Animations = tool:WaitForChild("Animations")
local min = tool.Min
local max = tool.Max

local animation1 = Animations:FindFirstChild("SwordIdle")
local animation2 = Animations:FindFirstChild("SwordSwing")

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local holdanim = humanoid:LoadAnimation(animation1)
local swinganim = humanoid:LoadAnimation(animation2)


local function Equipped()
	
		holdanim:Play()
end

local function Unequipped()
	
		holdanim:Stop()
end

local function Swing()
	
if not hit then 
	
	hit = true
	holdanim:Stop()
	swinganim:Play()
	wait(1.3)
	holdanim:Play()
	hit = false
	end
end

local function SwordCore(hit)
	if not debounce then
		
		debounce = true
			wait(0.2)
			
	local enemy = hit.Parent:FindFirstChild("Humanoid")
		if enemy then
			enemy.Health = enemy.Health - (math.random(min.Value, max.Value))
					wait(1.3)
					debounce = false
		end
	
		end
end


tool.Equipped:Connect(Equipped)
tool.Unequipped:Connect(Unequipped)
tool.Activated:Connect(Swing)
tool.Handle.Touched:Connect(SwordCore)
end)
end)

2 comments

0

Please, use proper indentation I can barely read the script.

0

What type of script is this?

1 answer

This will work in a normal script inside of a tool, I fixed up your original script and it looks a million times better

local debounce = false
local hit = false
local tool = script.Parent
local Animations = tool:WaitForChild("Animations")
local min = tool.Min
local max = tool.Max

local animation1 = Animations:FindFirstChild("SwordIdle")
local animation2 = Animations:FindFirstChild("SwordSwing")

local character
local humanoid
local holdanim
local swinganim

local function Equipped()
	local character = tool.Parent
	local humanoid = character:WaitForChild("Humanoid")
	local holdanim = humanoid:LoadAnimation(animation1)
	local swinganim = humanoid:LoadAnimation(animation2)
	holdanim:Play()
end

local function Unequipped()
	holdanim:Stop()
	character = nil
	humanoid = nil
	holdanim = nil
	swinganim = nil
end

local function Swing()
	if not hit then 
		hit = true
		holdanim:Stop()
		swinganim:Play()
		wait(1.3)
		holdanim:Play()
		hit = false
	end
end

local function SwordCore(hit)
	if debounce == false then
		debounce = true
		wait(0.2)

		local enemy = hit.Parent:FindFirstChild("Humanoid")
		if enemy then
			enemy:TakeDamage(math.random(min.Value, max.Value)) --TakeDamage() is the better way to do it.
			wait(1.3)
			debounce = false
		end
	end
end

tool.Equipped:Connect(Equipped)
tool.Unequipped:Connect(Unequipped)
tool.Activated:Connect(Swing)
tool.Handle.Touched(SwordCore)

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