Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I need to make a part that gives you a forcefield when you touch it. how do I do this?

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.

3 answers

✓ Accepted answer

Hello,

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)

3 comments

0

but I need to make the forcefield disappear when you stop touching the part

0

nvm I figured it out thx

0

No problem!

Hi

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)

1 comment

0

But How do I make it so that the forcefield disappears when you stop touching the part

This discussion is preserved as it was. New answers, comments, and votes are closed.