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

How to disable FF upon touching (part)?

Asked by 8 years ago

I have a forcefield script that works great. The thing is, I want it to disable after the user drops to the arena floor. He's my forcefield script:

Game:GetService"Players".PlayerAdded:connect(function(Player) 
    Player.CharacterAdded:connect(function(Character)
        Instance.new("ForceField",Character)
    end)
end)

With my lack of knowledge, I have no clue how to disable forcefield once the user touches the arena floor. I tried but it looked nothing like the above, and it broke the entire script. Any suggestions?

2 answers

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

Using a touched event, we can remove the ForceField from the character (but not before checking to see if it exists):

PART.Touched:connect(function(hit)
    if (hit.Parent:FindFirstChild("ForceField")) then
        hit.Parent.ForceField:Destroy()
    end
end)

Where PART is the location of the part object in the workspace (e.g. game.Workspace.FFRemover).

0
if you put a forcefield on a brick, it would destroy that forcefield from this script. So should you check if it is a humanoid. :P scottmike0 40 — 8y
0
You only need to check that case if it is relevant to the game.  Also, ForceFields are designed specifically for humanoids and only function properly given that it's parent has a descendant called Torso, therefore I don't really see anywhere such a case would exist.  BlackJPI 2658 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

You lack a wait for child in your method. And what you want to do is a touched event for that object, being the base..

Game:GetService"Players".PlayerAdded:connect(function(Player) 
    Player.CharacterAdded:connect(function(Character)
     Instance.new("ForceField",game.workspace:WaitForChild(Character.Name))//we need to get the name
    end)
end)

--add a Touched event for your floor
--the script should be inside floor
script.Parent.Touched:connect(function(player) //touched event allows you to give you the model of the player and using the paramater as other part
if(player.Humanoid ~=nil) then
player.ForceField:Destroy()-- calls the destroy function, which removes the ForceField that was parent
end

end)

a side note, : means after this that it is a function Every Object in roblox.lua has events, Each event requires :connect and passes in a paramater or a function that is made anon within the paramaters or outside the paramaters.

Answer this question