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

Tool keeps breaking?

Asked by 9 years ago

I just finished making a basic taser tool, the layout out being (Tool>Handle>LocalScript). Anyways, the script runs through fine when I'm testing it out in a test server, but whenever it makes contact with someone who's health is 0, it breaks.

So say for examples, Player1 uses the taser on Player2, and kills him. If Player1 hits one of Player2's limbs before he respawns, Player1's taser breaks and does work anymore until he resets. Here is the script:

debounce = false

script.Parent.Touched:connect(function(part)
    if not debounce then
        debounce = true
    local human = part.Parent:findFirstChild("Humanoid")
    local a = part.Parent:GetChildren()
    if (human == nil) then return end
    if human.Health <= 0 then return end
    if part.Parent.Head.face ~= nil then
        part.Parent.Head.face:Remove()
    end
    local d = Instance.new("Decal",part.Parent.Head)
    d.Texture = ("http://www.roblox.com/asset/?id=145854465")
    d.Face = ("Front")
    local s = Instance.new("Sparkles",part.Parent.Torso)
    for i=1,5 do
        for i,v in pairs(a) do
            if v:IsA("Part")then
                v.BrickColor = BrickColor.new("Teal")
            end
        end
        wait(0.1)
                for i,v in pairs(a) do
            if v:IsA("Part")then
                v.BrickColor = BrickColor.new("Really black")
            end
                end
                wait(0.1)
    end
    human.Health = 0
    s:Remove()
    debounce = false
    end
end)

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

Your debounce pattern is wrong.

Notice the early returns on line 8/9.

You don't set debounce to false, so forever after debounce will now be true and the taser will be disabled.

Move debounce = true to happen after your early returns.


EDIT: I caught another bug.

You can't use ~= nil to see if an object exists. Use :FindFirstChild instead.


Tab your code correctly! It makes it much easier to read.

  • Increase a tab when you start a new block
    • then, do, repeat, etc.
    • (blocks are groups of statements)
  • Decrease a tab when you end that block
    • end, until, etc
debounce = false

script.Parent.Touched:connect(function(part)
    if not debounce then
        local human = part.Parent:findFirstChild("Humanoid")
        local a = part.Parent:GetChildren()
        if not human or human.Health <= 0 then
            return
        end
        debounce = true
        if part.Parent.Head:FindFirstChild("face") then
            part.Parent.Head.face:Remove()
        end
        local d = Instance.new("Decal",part.Parent.Head)
        d.Texture = ("http://www.roblox.com/asset/?id=145854465")
        d.Face = ("Front")
        local s = Instance.new("Sparkles",part.Parent.Torso)
        for i = 1, 5 do
            for _, v in pairs(a) do
                if v:IsA("Part")then
                    v.BrickColor = BrickColor.new("Teal")
                end
            end
            wait(0.1)
            for _, v in pairs(a) do
                if v:IsA("Part")then
                    v.BrickColor = BrickColor.new("Really black")
                end
            end
            wait(0.1)
        end
        human.Health = 0
        s:Remove()
        debounce = false
    end
end)
0
Alright, thank you for correcting me, the fix worked and I know what why it broke. Also, when you say tab your code correctly, what do you mean? The tutorials I've been reading/watching haven't explained how to tab your code correctly. ghostblocks 74 — 9y
0
Increase an indent when you start a block -- after `do`, `then`, `repeat`, etc. Decrease an indent when you end a block -- starting at `end`, `until`, etc. BlueTaslem 18071 — 9y
0
Ok thanks. ghostblocks 74 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Personally, I would make the kill part of it a seperate function, such as

function kill(p)
if p.Parent.Humanoid ~= nil then
if p.Parent.Humanoid.Health ~= 0 then
if p.Parent.Head.Decal ~= nil then--might be face instead of decal, I generally don't deeal with more then the basic structure of players
p.Parent.Head.Decal:Destroy()
end
local d = Instance.new("Decal",part.Parent.Head)
d.Texture = ("http://www.roblox.com/asset/?id=145854465")
d.Face = ("Front")
char = p.Parent
local s = Instance.new("Sparkles", char.Torso)
xTime = 0
while xTime <= 5 do
for _, obj in pairs(char:GetChildren()) do
if v:IsA("Part")then
        v.Anchored = true --delete if you want, I just think a paralyzed pose would be cool
                v.BrickColor = BrickColor.new("Teal")
end
end
wait(0.1)
for _, obj in pairs(char:GetChildren()) do
if v:IsA("Part")then
        v.Anchored = true --delete if you want, I just think a paralyzed pose would be cool
                v.BrickColor = BrickColor.new("Really Black")
end
end
wait(0.1)
xTime = xTime+1
end
char.Humanoid.Health = 0
end
end
end

I just find it easier to edit that way, and you still get everything you want. also, to run it,

working = false
--declare function kill() here!
script.Parent.Touched:connect(function(part)
if working == false then
working = true
kill(part)
working = false
end

And remember to make sure the kill function is announced before it is called. I just didn't like the format of yours, and this should fix it up. That and the humanoid health is checked before hand so nothing breaks if stuff is already being destroyed in fire.

Answer this question