How to make a Forcefield System! | Scripting Tutorial

Heyo! This is a short tutorial on how to make a Forcefield System. It will have completely customizable color, size, force, and effects, now let’s get started!

Below is a video showing this in use:

1. Making the Module

  1. Make a ModuleScript inside ServerScriptService and name it ForcefieldHandler. Let’s write the main function
function module.new(Parent, Position, Size, Force)
    local Forcefield = Instance.new("Part")
    Forcefield.Name = "Forcefield"
    Forcefield.Shape = Enum.PartType.Ball --// You can change this, I'm making it a sphere
    Forcefield.Material = Enum.Material.ForceField
    Forcefield.Color = Color3.fromHex("#4ed0ff") --// You can change this, this is a light blue color
    Forcefield.Size = Size
    Forcefield.Position = Position
    Forcefield.Parent = Parent
    Forcefield.CanCollide = false
    Forcefield.Anchored = true

    local ForceValue = Instance.new("IntValue", Forcefield)
    ForceValue.Name = "Force"
    ForceValue.Value = 3 --// Default value | You can change this number

    return Forcefield
end

2. Using this system

This is a basic script creating a forcefield with a power of 50. It is listening for the .Touched event to be fired on the Forcefield, this example is applying a small knockback force.

local Debounce = false
local DebounceCount = 1

local ForcefieldModule = require(script.Parent.ModuleScript)
local Forcefield = ForcefieldModule.new(workspace, Vector3.new(-2, 2, 30), Vector3.new(20, 20, 20), 50)

Forcefield.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		if not Debounce then
			Debounce = true
			
			local Character = Hit.Parent
			local HRP = Character.HumanoidRootPart

			Character.Humanoid.UseJumpPower = true
			local JumpPower = 50

			if not HRP:FindFirstChild("ForcefieldKB") then
				local BodyVelocity = Instance.new("BodyVelocity")
				BodyVelocity.Name = "ForcefieldEffect"
				BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
				BodyVelocity.Velocity = (-HRP.Velocity) * Forcefield.Force.Value

				Character.Humanoid.PlatformStand = true
				Character.Humanoid.JumpPower = 0

				BodyVelocity.Parent = HRP

				task.wait(.1)

				BodyVelocity:Destroy()

				task.wait(1)

				Character.Humanoid.PlatformStand = false
				Character.Humanoid.JumpPower = JumpPower
			end
			
			task.wait(DebounceCount)
			Debounce = false
		end
	end
end)

Conclusion

If you have any questions or bugs, please reply with it below! I can help you fix any bugs that occur while using this module (unless it relates to a system you use in your game).

I got this idea from Roblox BedWars admins using /forcefield in custom matches

9 Likes

This is so so very useful! I can already see where I’m going to use it.

2 Likes

It will not work properly if you don’t walk straight into it, for example using shift lock.

Nice tutorial. Cool result. But, I must state this.

BodyVelocity is deprecated, probably better to set AssemblyLinearVelocity of the players torso directly.

Its probably better to use the RGB version of this, as most people dont really use hex colors that much. (at least on roblox)

3 Likes

I can swap it in the tutorial if needed, I’ve never heard of AssemblyLinearVelocity on Roblox before today haha.

1 Like

Haha yea I’ll switch it to a different method.

I tried to find one, but failed. Good luck.

Fixed it! The Force value will need to be a lot lower since I am using the HumanoidRootPart's negative velocity on touch.

1 Like

That should work regardless. I kind of needed that once. Thank you! I didn’t know I couldn’t think of that.

1 Like