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

How would you make a damage force field?

Hey, guys. I am trying to make a localscript that creates a force field (spherical part) around a player when they press the "f" key. I am also trying to make it so that this force field damages any other players that touch it.

In the following script, I have successfully made the force field pop up around the player whenever they press "f":

Player = game.Players.LocalPlayer
mouse = Player:GetMouse()

db = false
function onKeyDown(key)
key = key:lower()
if db == false then
	db = true
	if key == "f" then -- when F is pressed...
		--Make a little animation.
		local RightShoulder = Player.Character.Torso["Right Shoulder"]
		local LeftShoulder = Player.Character.Torso["Left Shoulder"]
		local explosion = Instance.new("Part", game.Workspace)
		explosion.Locked = true
		explosion.Anchored = true
		explosion.CanCollide = false
		explosion.CFrame = Player.Character.Torso.CFrame
		explosion.Transparency = 0.5
		explosion.FormFactor = "Symmetric"
		explosion.Size = Vector3.new(10,10,10)
		explosion.Shape = "Ball"
		explosion.BrickColor = BrickColor.new("Really red")
		for i = 1,10 do
 			RightShoulder.C0 = RightShoulder.C0 * CFrame.Angles(0,0,0.5)
			LeftShoulder.C0 = LeftShoulder.C0 * CFrame.Angles(0,0,-0.5)
			explosion.Size = explosion.Size + Vector3.new(1.2,1.2,1.2)
			wait(0.001)
		end
		for i = 1,10 do
 			RightShoulder.C0 = RightShoulder.C0 * CFrame.Angles(-0.5,0,0)
			LeftShoulder.C0 = LeftShoulder.C0 * CFrame.Angles(-0.5,0,0)
			wait(0.001)
		end
		for i = 1,10 do
 			RightShoulder.C0 = RightShoulder.C0 * CFrame.Angles(0.5,0,0)
			LeftShoulder.C0 = LeftShoulder.C0 * CFrame.Angles(0.5,0,0)
			wait(0.001)
		end
		for i = 1,10 do
 			RightShoulder.C0 = RightShoulder.C0 * CFrame.Angles(0,0,-0.5)
			LeftShoulder.C0 = LeftShoulder.C0 * CFrame.Angles(0,0,0.5)
			wait(0.001)
		end
		wait(3)
		for i = 1,10 do
			explosion.Transparency = explosion.Transparency + 0.1
			wait(0.001)
		end
		explosion:Destroy()
	end
	db = false
end
end
mouse.KeyDown:connect(onKeyDown)

But now, I have no idea how to make it so that any other player that touches the force field gets damaged. Not killed. Only damaged.

If anyone could explain to me how to accomplish this, I would gladly appreciate it!

1 answer

I did something similar to this, except I used it at the end of a gun.

So instead of detecting whoever touches it inside the current script, I'd rather just make a script inside the forcefield.

Like this:

local field = script.Parent
local damage = 10 --Change this to however much you want to damage players

function onTouch(part)
	if part.Parent:FindFirstChild("Humanoid") ~= nil then
		part.Parent.Humanoid:TakeDamage(damage)--Will not affect people with actual forcefields
	end
end

field.Touched:connect(onTouch)

Now you'd just need to put it in the forcefield, so I disable the script first, toss it into the Workspace, and I would put it in the forcefield this way:

local scriptClone = Workspace.Script:Clone()--I'm assuming you wouldn't change the name of the kill script
scriptClone.Parent = explosion
scriptClone.Disabled = false

Toss that code somewhere into the area where you are building the forcefield in the script. If the forcefield damages the player who created it, tell me and I'll give you another script.

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