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

Why is the last part firing only once?

Asked by 8 years ago

this is my full script:

tool = script.Parent
part = Instance.new("Part")
player = game.Players.LocalPlayer
stat = "undone"
h = "nope"

print("im black")
tool.Equipped:connect(function(mouse)
    mouse.KeyDown:connect(function(key)
        if stat == "undone" and key == "m" then
            stat = "done"
        print(key)
        part.Transparency = .5
        part.CanCollide = false

        local float = Instance.new("BodyPosition",part)
        float.MaxForce = Vector3.new(0,102999,0)
        local f = Instance.new("Fire",part)
        f.Color = Color3.new(0,0,0)
        f.SecondaryColor = Color3.new(255,0,0)
        f.Heat = 25
        f.Size = 30
        local f1 = Instance.new("Fire",part)
        f1.Color = Color3.new(0,0,0)
        f1.SecondaryColor = Color3.new(255,0,0)
        f1.Heat = 25
        f1.Size = 30
        local f2 = Instance.new("Fire",part)
        f2.Color = Color3.new(0,0,0)
        f2.SecondaryColor = Color3.new(255,0,0)
        f2.Heat = 25
        f2.Size = 30
        local f3 = Instance.new("Fire",part)
        f3.Color = Color3.new(0,0,0)
        f3.SecondaryColor = Color3.new(255,0,0)
        f3.Heat = 25
        local f4 = Instance.new("Fire",part)
        f4.Color = Color3.new(0,0,0)
        f4.SecondaryColor = Color3.new(255,0,0)
        f4.Heat = 25
        f4.Size = 30
        local f5 = Instance.new("Fire",part)
        f5.Color = Color3.new(0,0,0)
        f5.SecondaryColor = Color3.new(255,0,0)
        f5.Heat = 25
        f5.Size = 30
        local f6 = Instance.new("Fire",part)
        f6.Color = Color3.new(0,0,0)
        f6.SecondaryColor = Color3.new(255,0,0)
        f6.Heat = 25
        f6.Size = 30
        local f7 = Instance.new("Fire",part)
        f7.Color = Color3.new(0,0,0)
        f7.SecondaryColor = Color3.new(255,0,0)
        f7.Heat = 25
        f7.Size = 30
        local f8 = Instance.new("Fire",part)
        f8.Color = Color3.new(0,0,0)
        f8.SecondaryColor = Color3.new(255,0,0)
        f8.Heat = 25
        f8.Size = 30
        local f9 = Instance.new("Fire",part)
        f9.Color = Color3.new(0,0,0)
        f9.SecondaryColor = Color3.new(255,0,0)
        f9.Heat = 25
        f9.Size = 30
        local f10 = Instance.new("Fire",part)
        f10.Color = Color3.new(0,0,0)
        f10.SecondaryColor = Color3.new(255,0,0)
        f10.Heat = 25
        f10.Size = 30
        local f11 = Instance.new("Fire",part)
        f11.Color = Color3.new(0,0,0)
        f11.SecondaryColor = Color3.new(255,0,0)
        f11.Heat = 25
        f11.Size = 30
        local f12 = Instance.new("Fire",part)
        f12.Color = Color3.new(0,0,0)
        f12.SecondaryColor = Color3.new(255,0,0)
        f12.Heat = 25
        f12.Size = 30       
        local f13 = Instance.new("Fire",part)
        f13.Color = Color3.new(0,0,0)
        f13.SecondaryColor = Color3.new(255,0,0)
        f13.Heat = 25
        f13.Size = 30       
        local head = script.Parent.Parent.Head
        part.CFrame = player.Character.Torso.CFrame * CFrame.new(0,2,-5)
        part.Size = Vector3.new(10,10,10)
        part.Shape = "Ball"
        part.BrickColor = BrickColor.new("Really black")
        part.Velocity = player.Character.Torso.CFrame.lookVector * 120
        local bod = Instance.new("BodyVelocity")
        bod.MaxForce = Vector3.new(30,30,30)
        bod.Velocity = player.Character.Torso.CFrame.lookVector * 120
         part.Parent = game.Workspace


        bod.Parent = part
        wait(4)
        part:Remove()
        stat = "undone"
            end
        end)


end)
part.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil and h == "nope" then
        h = "Yep"
        local h = hit.Parent:FindFirstChild("Humanoid")
        h:TakeDamage(2)
        print("it has been dont-"..h.Health)
        wait(2)
        h = "nope"
    end
end)






But i want to focus on the main issue

if hit.Parent:FindFirstChild("Humanoid") ~= nil and h == "nope" then
        h = "Yep"
        local h = hit.Parent:FindFirstChild("Humanoid")
        h:TakeDamage(2)
        print("it has been done-"..h.Health)
        wait(2)
        h = "nope"
    end

Why is it that this part fires only once

2 answers

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
8 years ago

Your problem is that you redefined what the variable 'h' is, so the original variable 'h' never gets changed. The solution is simple, change the name of one of the variables.

part.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil and h == "nope" then
        h = "Yep"
        local humanoid = hit.Parent:FindFirstChild("Humanoid") -- Changed h to humanoid
        humanoid:TakeDamage(2)
        print("it has been done-"..humanoid.Health)
        wait(2)
        h = "nope"
    end
end)

Edit

Due to your request for an overall solution, I went through and created a script that will do what I believe that you want to happen. This script is more organized, and works great.

What I did first was set all the variables to local. This is a good habit to get into as it will help to prevent variable leaks (like the one that I fixed in my original answer).

After that I used the Equipped and Unequipped events to manage the player and character variables and used the Activated event to determine when the player left clicks.

Next I created a function for creating the fire objects. This reduced down your repeated lines of new fire instances into a short function and a 3 line for loop.

Now is just a matter of creating the part. I used the exact parameters you had, but instead of using BodyMover objects to keep the part going at the same velocity I used a repeat loop. Using the difference between 'tick()'s I am able to determine when four seconds has passed and the user is able to fire again. I used the Debris Service so that the part would automatically be destroyed when four seconds is up (Warning: If the player leaves before the four seconds is up, then the block will not be destroyed. If you want to prevent this, then I would suggest moving the activated event function to a server script instead of a local script).

local tool = script.Parent
local player, character
local projectileDebounce = false
local touchedDebounce = false

local function CreateFire(parent, color, secondaryColor, heat, size)
    local fire = Instance.new("Fire", parent)
    fire.Color = color
    fire.SecondaryColor = secondaryColor
    fire.Heat = heat
    fire.Size = size
    return fire
end

tool.Equipped:connect(function(mouse)
    player = game.Players.LocalPlayer
    character = player.Character
end)

tool.Activated:connect(function()
    -- Fires on Left Click
    if not projectileDebounce then
        projectileDebounce = true
        local part = Instance.new("Part", game.Workspace)
        local velocity = character.Torso.CFrame.lookVector * 120
        part.CFrame = character.Torso.CFrame * CFrame.new(0, 2, -5)
        part.Size = Vector3.new(10, 10, 10)
        part.Shape = Enum.PartType.Ball
        part.BrickColor = BrickColor.new("Really black")
        part.Velocity = velocity

        part.Touched:connect(function(hit)
            local humanoid = hit.Parent:FindFirstChild("Humanoid")
            if humanoid and not touchedDebounce then
                touchedDebounce = true
                humanoid:TakeDamage(2)
                wait(2)
                touchedDebounce = false
            end
        end)

        for i = 1, 13 do
            CreateFire(part, Color3.new(), Color3.new(1, 0, 0), 25, 35)
        end
        game.Debris:AddItem(part, 4)

        local firstTick = tick()
        repeat
            if part then
                part.Velocity = velocity
            end
            game["Run Service"].RenderStepped:wait()
        until elapsed >= 4

        projectileDebounce = false
    end
end)

tool.Unequipped:connect(function()
    player = nil
    character = nil
end)
0
the variable situtaion was a mistake on my part, dont know how i missed that, but this event still only fires once. Even with the fixed debounce it still fails to fire for each part shot koolkid8099 705 — 8y
0
here is the fixed version : http://prntscr.com/8cqvg9 koolkid8099 705 — 8y
0
That's because part is a variable referencing the very first part created in the second line of your script and only that individual instance BlackJPI 2658 — 8y
0
any suggestions koolkid8099 705 — 8y
View all comments (2 more)
0
See my edit BlackJPI 2658 — 8y
0
thx koolkid8099 705 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Problem In line 3 of the second script you make a new variable of h which was the debouncers variable, this over wrote it making h a humanoid so when it reaches the if statement since h is a humanoid then it won't do anyhting. This is fixable by renaming line 3 variable or the debouncer.

Answer this question