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

How do I debounce this damage?

Asked by 8 years ago
wait(1)

local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local humanoid = player.Character.Humanoid 
local s = humanoid:LoadAnimation(game.StarterPack.Fireball.Animation)

Player = script.Parent.Parent  
Character = Player.Character 
Mouse = Player:GetMouse()  
usable = true

function onKeyDown(key) 
    key = key:lower() 
    if key == "f" and usable then 
    usable = false 
        s:Play()
        player.Character.Humanoid.WalkSpeed = 16
        wait(.73)
        local x = Instance.new("Part")
        x.TopSurface = "Smooth" 
        x.BottomSurface = "Smooth" 
        x.Shape = "Ball" 
        x.Transparency = .7
        x.BrickColor = BrickColor.new("Bright red")
        x.Size = Vector3.new(10, 10, 10)
        local z = Instance.new("Fire") 
        z.Parent = x 
        z.Size = 12 
        z.Heat = 12
        local p = Instance.new("ParticleEmitter")
        p.Parent = x
        p.Texture = 'rbxgameasset://Images/Fireball'
        p.Lifetime = NumberRange.new(.5,1)
        p.Size = NumberSequence.new(5)
        p.Transparency = NumberSequence.new(.5)
        p.Rate = 50
        local y = Instance.new("BodyVelocity") 
        y.maxForce = Vector3.new(math.huge, math.huge, math.huge)
        y.velocity = Character.Torso.CFrame.lookVector*80
        y.Parent = x  
        x.Parent = game.Workspace 
        x.CFrame = Character.Torso.CFrame*CFrame.new(0, 0, -12)

        x.Touched:connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") then
                hit.Parent.Humanoid:TakeDamage(20)
                if x then -- Just in case, to counter the event that the fireball gets deleted before this.
                    x:Destroy()
                end
            end
        end)

        wait(3.5)
        x:remove()
    wait(.6) 
    usable = true 
        end
    end

Mouse.KeyDown:connect(onKeyDown)

This is supposed to either disappear after 3.5 seconds, or disappear after it touches a humanoid. It disappears after 3 seconds anyway. So whenever I shoot this at someone, it automatically kills them because this script plays over and over, doing 20 damage over and over.

How do I make this thing disappear after touching a player?

1 answer

Log in to vote
0
Answered by
lucas4114 607 Moderation Voter
8 years ago
wait(1)

local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local humanoid = player.Character.Humanoid 
local s = humanoid:LoadAnimation(game.StarterPack.Fireball.Animation)

Player = script.Parent.Parent  
Character = Player.Character 
Mouse = Player:GetMouse()  
usable = true

local debounce = false

function onKeyDown(key) 
    if debounce == false then
      debounce = true
        key = key:lower() 
        if key == "f" and usable then 
        usable = false 
            s:Play()
            player.Character.Humanoid.WalkSpeed = 16
            wait(.73)
            local x = Instance.new("Part")
            x.TopSurface = "Smooth" 
            x.BottomSurface = "Smooth" 
            x.Shape = "Ball" 
            x.Transparency = .7
            x.BrickColor = BrickColor.new("Bright red")
            x.Size = Vector3.new(10, 10, 10)
            local z = Instance.new("Fire") 
            z.Parent = x 
            z.Size = 12 
            z.Heat = 12
            local p = Instance.new("ParticleEmitter")
            p.Parent = x
            p.Texture = 'rbxgameasset://Images/Fireball'
            p.Lifetime = NumberRange.new(.5,1)
            p.Size = NumberSequence.new(5)
            p.Transparency = NumberSequence.new(.5)
            p.Rate = 50
            local y = Instance.new("BodyVelocity") 
            y.maxForce = Vector3.new(math.huge, math.huge, math.huge)
            y.velocity = Character.Torso.CFrame.lookVector*80
            y.Parent = x  
            x.Parent = game.Workspace 
            x.CFrame = Character.Torso.CFrame*CFrame.new(0, 0, -12)

            x.Touched:connect(function(hit)
                if hit.Parent:FindFirstChild("Humanoid") then
                    hit.Parent.Humanoid:TakeDamage(20)
                    if x then -- Just in case, to counter the event that the fireball gets deleted before this.
                        x:Destroy()
                    end
                end
            end)

            wait(3.5)
            x:remove()
        wait(.6) 
        usable = true 
            end
     debounce = false
    end
 end

Mouse.KeyDown:connect(onKeyDown)


Ad

Answer this question