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

Why am I still dying even when I have a ForceField and not dying when I don't?

Asked by 9 years ago

This script is inside a part. And when you touch without a ForceField you lose 100 health. (There is a shop where you can buy more health. That's why you lose 100 health instead of just setting health to 0) And if you touch it with a ForceField, you're supposed to not lose any health and have a 25% chance of getting 100 points.

debounce = false
script.Parent.Touched:connect(function(hit)
    if not hit then
        return true
    elseif not hit.Parent then
        return true
    end
    local h = hit.Parent:findFirstChild("Humanoid")
    local ff = hit.Parent:FindFirstChild("ForceField1"),hit.Parent:FindFirstChild("ForceField2"),hit.Parent:FindFirstChild("ForceField3"),hit.Parent:FindFirstChild("ForceField4")
    local nff1 = hit.Parent:FindFirstChild("ForceField1",false)
    local nff2 = hit.Parent:FindFirstChild("ForceField2",false)
    local nff3 = hit.Parent:FindFirstChild("ForceField3",false)
    local nff4 = hit.Parent:FindFirstChild("ForceField4",false)
    local p = hit.Parent:findFirstChild("Grass"),hit.Parent:findFirstChild("Rock")
    if h then
        if ff then
            print (hit.Parent)--Player
            local points = game.Players:GetPlayerFromCharacter(hit.Parent):WaitForChild("Points")
            if hit.Parent.RocksLeft.Value ~= 0 then
                hit.Parent:WaitForChild("RocksLeft").Value = hit.Parent.RocksLeft.Value - 1
            else
                ff:Destroy()
            end
            local var1 = 1
            local var2 = math.random(1,4)
            if var2 == var1 then
                points.Value = points.Value + 100
            end
            script.Parent:Destroy()
        elseif nff1 or nff2 or nff3 or nff4 then
            if debounce == false then
                debounce = true
                h.Health = h.Health - 100
                print 'rip'
                script.Parent:Destroy()
            end
            wait(.5)
            debounce = false
        end
    elseif p then
        local var = math.random(1,3)
        if var == 1 then
            script.Parent:Destroy()
        end
    end
end)

The problem is that when you touch it with a ForceField you loose 100 health, and when you touch it without a ForceField you don't lose any health.

1 answer

Log in to vote
0
Answered by 9 years ago

First thing I see is that you are checking if a tuple is true in your if statement. Second I believe that you are using the second argument in the FindFirstChild method wrong. The second argument specifies whether to search recursively or not.(It defaults to false)

I created a function to make sure that we find all possible forcefields.

local debounce = false

function FindFF(Char)
    local T = {}
    for _,n in pairs(Char:GetChildren()) do
        if n:IsA("ForceField") then
            table.insert(T,n)
        end
    end
    return T
end

script.Parent.Touched:connect(function(hit)
    local char = hit.Parent
    local h = char:FindFirstChild("Humanoid")
    local ff = FindFF(char)
    local p = char:FindFirstChild("Grass") or char:FindFirstChild("Rock")
    if h then
        local rocks = char:WaitForChild("RocksLeft")
        if #ff > 0 then --If there is more than one forcefield
            local points = game.Players:GetPlayerFromCharacter(char):WaitForChild("Points")
            if rocks.Value > 0 then
               rocks.Value = rocks.Value - 1
            else
                --To only destroy one forcefield do:
                --ff[1]:Destroy()
                --To remove all forcefields to:
                --[[
                    for _,n in pairs(ff) do
                        n:Destroy()
                    end
                --]]
            end 
            if math.random(1,4) == 1 then 
                points.Value = points.Value + 100
            end
            script.Parent:Destroy()
        else
            if debounce == false then
                debounce = true
                h.Health = h.Health - 100
                print 'rip'
                script.Parent:Destroy()
                wait(.5)
                debounce = false
            end            
        end
    elseif p then
        if math.random(1,3) == 1 then
            script.Parent:Destroy()
        end
    end
end)
Ad

Answer this question