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
- Make a
ModuleScript
insideServerScriptService
and name itForcefieldHandler
. 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