I need to make a script that gives the player a forcefield when they touch a part and destroy the forcefield when they stop touching the part. I realized the script below wouldn't work until halfway through writing it.
local ActivationPart = script.Parent if ActivationPart.Touched == true then local Force = Instance.new("ForceField") Force.Parent = //This is where I realized this wouldn't work else Force:Destroy() end
I'm new to Lua and really suck at it, so sorry that this script is trashy.
I've done your script Also the problem with the one from imnotaguest1121 is that it creates a bunch and also it doesn't go after 5 seconds.
local DebrisService = game:GetService("Debris") -- Using debris so we don't yield the code script.Parent.Touched:Connect(function(hit) if hit and hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:FindFirstChild("ForceField") then local forcefield = Instance.new("ForceField") forcefield.Parent = hit.Parent DebrisService:AddItem(forcefield, 5) -- Remove the Forcefield after 5 seconds --[[ If you don't want to use Debris then just do wait(5) forcefield:Destroy() ]] end end)
local Part = script.Parent ---The part Part.Touched:Connect(function(hit) ---A working event that happens If a player or NPC touches the part if hit.Parent:FindFirstChild("Humanoid") then ---If the part Is touching a player or NPC local ForceField = Instance.new("ForceField") ---Create a forcefield ForceField.Parent = hit.Parent ---Parent the forcefield to the player or NPC who touched the part ---OPTIONAL--- --[[ game:GetService("Debris"):AddItem(ForceField,3) ---Wait 3 seconds (In roblox time) before destroying Itself Also DON'T just simply use: wait(1) ForceField:Destroy() The reason Is because It can yield, not making the forcefield destroy Itself ]] end end)