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

Problem with a forcefield giver?

Asked by
Relatch 550 Moderation Voter
8 years ago

My forcefield giver doesnt give the forcefield when a player touches it. I want it to check if a player already has a forcefield. If they don't, it gives them one.

script.Parent.Touched:connect(function(hit)
    local h = hit.Parent:FindFirstChild("Humanoid")
    if h.Parent:FindFirstChild("ForceField") == nil then
        local ff = Instance.new("ForceField")
        ff.Parent = h
    end
end)

2 answers

Log in to vote
2
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
8 years ago

You're parenting the ForceField to the Humanoid instead of the Character Model. As well, you never actually check if h is not nil:

script.Parent.Touched:connect(function(hit)
    local h = hit.Parent:FindFirstChild("Humanoid")
    if h and not hit.Parent:FindFirstChild("ForceField") then
        Instance.new("ForceField", hit.Parent)
    end
end)
Ad
Log in to vote
-2
Answered by 8 years ago

The problem is that you need to make sure hit is nil by using ~=nil ****FINAL CODE

function onTouched(hit)
local h = hit.Parent:findFirstChild("Humanoid")
if h ~= nil then
local f = Instance.new("ForceField")
f.Parent = hit.Parent
end
end

script.Parent.Touched:connect(onTouched)

Answer this question