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!
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
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
UGH, yeah ur right. thats one thing i completely forgot. “skipping players w a forcefield” . 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
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)