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

How do i get a list of the position of every humanoid's torso in the workspace?

I need a function to find the position of every torso in the game and return the distance between each torso and the client player's torso. i have no idea how to find all the torso's in the game, could someone help with this?

4 comments

0

SH is not a request site. Please include the code you have even if it does not work.

0

i allready know how to find the distance from characters, i just need to know how to find the characters in the first place

1

Then you have the code which should be included in your question.

2 answers

✓ Accepted answer

As aforementioned by @kingdom5, this is not a request site. I am among a short amount of users who are still willing to aid you in your work, however, this will not always be a guarantee. Please try and make your own attempt first, or include the code you've already tried to write.

You can create a dictionary holding each affiliated Torso to the respectful Player. But, I suggest using the RootPart instead, as it is universal across all Character Rigs. This way we don’t have to apply compensation, while still getting identical results.

local Players = game:GetService("Players")

function GetCharacterRootPositions()
    local RootPositionList = {};
    for _,Player in pairs(Players:GetPlayers()) do
        if (Players[Player]) then
            local Character = Player.Character or Player.CharacterAdded:Wait(.5)
            local RootPart = Character:FindFirstChild("HumanoidRootPart")
            if (Character and RootPart) then
                RootPositionList[Player] = RootPart.Position
            end
        end
    end
    return RootPositionList 
end

local CharacterRootPositions = GetCharacterRootPositions()
--// print(CharacterRootPositions[Players.Feahren])

You can then use a pairs loop to iterate through each Vector3 and determine the Magnitude distances.

6 comments

0

This is not a request site.

0

It doesn't matter kingdom5. I can choose to answer if I please, there is nothing wrong with my answer either so I polity ask you to revoke your unnecessary downvote.

0

Please review https://scriptinghelpers.org/help/community-guidelines users should not promote actions that are agains community guidelines.

0

I am very aware of the guidelines, even after skimming it, I still do not see any statement against my actions. I appreciate that you wish to enforce and uphold these guidelines, yet this is my decision. Again, please revoke your downvote.

0

im not requesting anything other than an answer to the question of how to do something. all i was asking was how to program something. when i said "i need a function" i meant that i needed to make a function and needed to know how to do a specific thing to make said function. i was not "requesting" anything.

0

I upvoted this answer (sometimes you don't have any idea on how to start, and how would you "guess" at the solution?), but see my answer for a revised script - you don't want to wait for a character to load in (what if they don't ever load in, for instance?), nor do you want to stop the loop if a player somehow didn't exist (which won't happen in the first place)

This is a modification of Feahren's script; not meant to be an answer (can't write a script in a comment):

local Players = game:GetService("Players")

function GetCharacterTorsoPositions()
    local TorsoPositionList = {};
    for _,Player in pairs(Players:GetPlayers()) do
        -- if not (Player) then return end -- This line is unneeded because Player will always exist (or else GetPlayers would not have returned it)
        local Character = Player.Character -- or Player.CharacterAdded:Wait() -- disabling the Wait because if a player happened to leave the game while their character doesn't exist (if this is possible), you don't want the script to hang forever. Even if the event does fire, there's no need to wait for a position for every player - just get the positions of players that *are* spawned in
        if Character --[[and Players[Player])]] then -- no need to re-check if the player is in the game
            local Torso = Character:FindFirstChild("Torso") or Character:FindFirstChild("UpperTorso")
		if Torso then -- we *do* have to check for this - they may have a dead character whose Torso despawned
	            TorsoPositionList[Player] = Torso.Position
		end
        end
    end
    return TorsoPositionList 
end

local CharacterTorsoPositions = GetCharacterTorsoPositions()
--// print(CharacterTorsoPositions[Players.Feahren])

Without the added comments/disabled parts it is:

local Players = game:GetService("Players")

function GetCharacterTorsoPositions()
    local TorsoPositionList = {};
    for _,Player in pairs(Players:GetPlayers()) do
        local Character = Player.Character
        if Character then
            local Torso = Character:FindFirstChild("Torso") or Character:FindFirstChild("UpperTorso")
		if Torso then
	            TorsoPositionList[Player] = Torso.Position
		end
        end
    end
    return TorsoPositionList 
end

local CharacterTorsoPositions = GetCharacterTorsoPositions()
--// print(CharacterTorsoPositions[Players.Feahren])

Also, you could use HumanoidRootPart instead of looking for a Torso or UpperTorso, since all character models have that.

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