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

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

Asked by 2 years ago

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

Log in to vote
0
Answered by
MattVSNNL 620 Moderation Voter
2 years ago

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)
0
but I need to make the forcefield disappear when you stop touching the part LAVA_9799 11 — 2y
0
nvm I figured it out thx LAVA_9799 11 — 2y
0
No problem! MattVSNNL 620 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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)
0
But How do I make it so that the forcefield disappears when you stop touching the part LAVA_9799 11 — 2y
Log in to vote
0
Answered by 2 years ago

``dsa

Answer this question