hello! i have a script that checks for my username etc when i join. this works but im trying to see if a player is within a distance from me, then they take damage.
would i have to return my character to be used in another function? im not sure how to go ab this ty!
function distancekillModule.playerAdded3()
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
if plr.UserId == 64032380 or plr.Name == "targetframed" then
print("check complete, "..plr.Name.. " has joined the server")
end
end)
end)
end
Using a for loop, you can iterate through every player and check their distance using the magnitude of the difference between the two position vectors (yours and the other players). Here’s an example:
for _, _plr in ipairs(game.Players:GetPlayers())
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 <= 20) then
local hum = plrChar:FindFirstChild("Humanoid")
if (hum) then hum:TakeDamage(10) end -- Change number to damage you want player to take
end
end
You specifically want a Player-Character’s distance, so I’ll add a different magnitude check than people are suggesting, just for simplicity:
local OtherPlayerPosition
local Magnitude = game.Players.Player:DistanceFromCharacter(OtherPlayerPosition)
Basically you just need the Player instance of a user, and a Vector3 (position) of what you want a distance for. Magnitude is your distance in studs and does the same thing as
hey rumble, tysm for ur help. i really appreciate it.
i cant seem to get it to work unfortunately tho. would i have to use like say renderstepped / stepped from the run service?
i did this:
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")
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 <= 20) 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
Oh, I guess I misinterpreted your initial question. If you’re wanting to constantly check if a player is nearby then a while loop should be fine.
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")
while (plr.Character == char) do -- Check if the current character is this character model
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 <= 20) then
local hum = plrChar:FindFirstChild("Humanoid")
if (hum) then hum:TakeDamage(10) end -- Change number to damage you want player to take
end
end
task.wait(0.1)
end
end
end)
end)
end
that explanation is so underrated. u explained that incredibly well, ty.
i tried it like this but i’m not sure what’s going wrong. im getting no error in the output so its running but like its not working.
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")
local myplrPos = nil
local otherplrPos = nil
for _, otherPlr in pairs(game.Players:GetPlayers()) do
if plr == otherPlr then continue end
local magnitude = plr:DistanceFromCharacter(otherPlr)
myplrPos = char:GetPivot().Position
otherplrPos = otherPlr.Character:GetPivot().Position
if ((myplrPos - otherplrPos).Magnitude) <= 20 then
if otherPlr.Character.Humanoid.Health > 0 then
otherPlr.Character.Humanoid.Health = 0
end
end
end
end
end)
end)
end
it works absolutely perfectly, thank u so much. sorry i was the one that explained what i wanted poorly. would using one of the RS events be better than a while true do loop performance wise? tysm man!
The value you pass through the function should be a Vector3 value. otherPlr is a Player instance. It should be:
local magnitude = plr:DistanceFromCharacter(otherPlr:GetPivot().Position)
if (magnitude <= 20) then
-- rest of code
end
As for your other question, there is very little performance difference with something like calculations. It’s not big enough to cause issues. While loops also give you the option to add a larger delay (say you wanted to wait 1 or 2 seconds) between each loop whereas Stepped and Heartbeat runs every frame.
absolute legend. i got both ways to work. super cool. tysm! u’ve helped me w a gazillion things man, i hope i can return the favour when you’re stuck. tysm