Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Tool breaks when touching a part?

Asked by 4 years ago

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)
0
Please, use proper indentation I can barely read the script. firestarroblox123 440 — 4y
0
What type of script is this? firestarroblox123 440 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

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)
Ad

Answer this question