Trying to kill a player even if they have a forcefield

i have a script that kills a player if im within the distance threshold but if the player has a forcefield it won’t work. the script works when they dont have a forcefield. i want my script to kill the players even tho they have a forcefield. tysm! :smiley:

script:

function distancekillModule.generateForcefield()
	game.Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function(char)
			local forcefield = Instance.new("ForceField", char)
			forcefield.Visible = true
			wait(5)
			forcefield.Visible = false
		end)
	end)
end

function distancekillModule.playerAdded3()
	game.Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function(char)
			if plr.UserId == 64032380 or plr.Name == "targetframed" or plr.Name == "Player1" then
				print("check complete, "..plr.Name.. " has joined the server")
				if plr.Character == char then
					rs.Stepped:Connect(function()
						for _, _plr in ipairs(game.Players:GetPlayers()) do
							if (plr == _plr) then continue end -- Skip this player, this is yourself
							local plrChar = _plr.Character
							if (plrChar == nil) then continue end -- Player has no character to reference
							if plrChar:FindFirstChildOfClass("ForceField") or _plr:FindFirstChildOfClass("ForceField") then continue end
							-- You can change the distance but I'm setting it as 20
							if (plrChar:GetPivot().Position - char:GetPivot().Position).Magnitude <= 30 then
								local hum = plrChar:FindFirstChild("Humanoid")
								if (hum) then hum:TakeDamage(10) end -- Change number to damage you want player to take
							end 
						end
					end)
				end
			end
		end)
	end)
end
2 Likes

It seems this line prevent players with forced field to take damage:

if plrChar:FindFirstChildOfClass("ForceField") or _plr:FindFirstChildOfClass("ForceField") then continue end
3 Likes

Hi again! As the post above states, there’s a line in the block of code that skips the current index if they have a forcefield. Although, that’s not the only reason preventing it. Humanoid:TakeDamage also takes into account the forcefield and will not damage the player if they have it.

You could just remove the if statement that checks if the player has a forcefield and also change:

if (hum) then hum:TakeDamage(10) end -- Change number to damage you want player

to:

if (hum) then hum.Health -= 10 end -- Change number to damage you want player
3 Likes

UGH, yeah ur right. thats one thing i completely forgot. “skipping players w a forcefield” :man_facepalming:. tysm!

one thing is tho is that it still wont kill players with a forcefield. any way to tackle this?

(same code except i removed the line:

function distancekillModule.generateForcefield()
	game.Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function(char)
			local forcefield = Instance.new("ForceField", char)
			forcefield.Visible = true
			wait(5)
			forcefield.Visible = false
		end)
	end)
end

function distancekillModule.playerAdded3()
	game.Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function(char)
			if plr.UserId == 64032380 or plr.Name == "targetframed" or plr.Name == "Player1" then
				print("check complete, "..plr.Name.. " has joined the server")
				if plr.Character == char then
					rs.Stepped:Connect(function()
						for _, _plr in ipairs(game.Players:GetPlayers()) do
							if (plr == _plr) then continue end -- Skip this player, this is yourself
							local plrChar = _plr.Character
							if (plrChar == nil) then continue end -- Player has no character to reference
							-- You can change the distance but I'm setting it as 20
							if (plrChar:GetPivot().Position - char:GetPivot().Position).Magnitude <= 30 then
								local hum = plrChar:FindFirstChild("Humanoid")
								if (hum) then hum:TakeDamage(10) end -- Change number to damage you want player to take
							end 
						end
					end)
				end
			end
		end)
	end)
end
2 Likes

Another thing, if you plan on using Stepped, make sure you disconnect the connection once the character is no longer the same as the player’s Character property. Otherwise, you’ll have this constantly running and that is not good!

local connection
connection = rs.Stepped:Connect(function()
    if (not plr:IsDescendantOf(game.Players) or plr.Character ~= char) then -- We check if the player is still in the game or the player's character is different from this character model
        connection:Disconnect() -- Disconnects the connection
    end

	for _, _plr in ipairs(game.Players:GetPlayers()) do
		if (plr == _plr) then continue end -- Skip this player, this is yourself
		local plrChar = _plr.Character
		if (plrChar == nil) then continue end -- Player has no character to reference
		if plrChar:FindFirstChildOfClass("ForceField") or _plr:FindFirstChildOfClass("ForceField") then continue end
		-- You can change the distance but I'm setting it as 20
		if (plrChar:GetPivot().Position - char:GetPivot().Position).Magnitude <= 30 then
			local hum = plrChar:FindFirstChild("Humanoid")
			if (hum) then hum:TakeDamage(10) end -- Change number to damage you want player to take
		end 
	end
end)

Also you can get rid of:

if plr.Character == char then

before the Stepped event as well. :slight_smile:

2 Likes

rumble ur on every one of my posts i swear. ur always helping me out tysm. :sob:

thank u so so much it worked. ofc it had to be math operators. just read up about the operators rn tysm. i understand it now! :smiley:

that’s really good to know. tysm 4 the info! 100% will take that into account.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.