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

I have a script that gives a player a forcefield if they are inside a part, why isnt it working?

Asked by 3 years ago
function onTouch(p)

    local h = p.Parent:FindFirstChild("Humanoid")
    if h then

    local ff = p.Parent:findFirstChild("ForceField") 

    if h.Health == 100 and not ff then

    Instance.new("ForceField", h.Parent)

    script.Parent.TouchEnded:connect(function()

        if ff then

            Instance.new("ForceField", h.Parent):Destroy()

        end

    end)

    end
    end
    end

script.Parent.Touched:connect(onTouch)

can someone please tell me why this isnt working

0
Move the TouchEnded function out of the Touch function WoTrox 345 — 3y
0
I'm not new to scripting, i'm just really bad at it. Can you tell me where to put it? myrukun 2 — 3y

1 answer

Log in to vote
0
Answered by
WoTrox 345 Moderation Voter
3 years ago
function onTouch(p)

    local h = p.Parent:FindFirstChild("Humanoid")

    if h then

        local ff = p.Parent:FindFirstChild("ForceField") 

        if h.Health == 100 and not ff then

            local newff = Instance.new("ForceField", p.Parent)

        end
    end
end

function onTouchEnded(p)

    local h = p.Parent:FindFirstChild("Humanoid")

    if h then

        local ff = p.Parent:FindFirstChild("ForceField") 

        if ff then

            ff:Destroy()

        end
    end
end

script.Parent.Touched:Connect(onTouch)
script.Parent.TouchEnded:Connect(onTouchEnded)

The problem was that you destroyed a new ForceField, not the one that the player had. Also, look out for lowercase and uppercase characters, as some functions may not work if not written correctly.

Ad

Answer this question