-
What do you want to achieve? I’m trying to get characters (models) in workspace simple as that.
-
What is the issue? Can’t seem to figure it out how much i try.
-
What solutions have you tried so far? I tried both
for _, v in pairs(workspace:GetChildren()) doandfor _, v in pairs(workspace:GetDescendants()) doand they seem to be looping through ALL of workspaces instead of just character models.
Thanks in advance!
loop through the Game.Players item instead and do player.Character
HeYSeN’s code probably works better tbh
Oh hey elomala has the same idea as me
You can still use for _, v in pairs(workspace:GetChildren()) do but you need to add a character check to see if it is a character but not another part. I usually do it like this:
ye elomala’s code is better
for _, v in pairs(workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") and game.Players:FindFirstChild("v.Name") then
--your code here
end
end
try this:
local characters = {}
for i, player in pairs(game.Players:GetChildren())
local char = player.Character
table.insert(characters, char)
end
-- The characters table will have all the characters in workspace
putting .Character after the player allows you to get the character of the player.
Both @elomala and @HeYSeNHeLlO_TR would work But if a Character Gets added late they won’t be parented to workspace.
Try this instead.
for i,v in pairs(game.Workspace:GetDescendants()) do
local plr = game.Players:GetPlayerFromCharacter(v)
if plr then
plr.Character.Parent = game.Workspace
end
end
game.Workspace.ChildAdded:Connect(function(v)
print(v)
local plr = game.Players:GetPlayerFromCharacter(v)
if plr then
plr.Character.Parent = game.Workspace
end
end)
oh yes why not use the playeradded function instead?
Just like @PANDASonNOOB said, use player added. Here is a cleaner version of the code using functions aswell:
local Players = game:GetService("Players")
local chars = {}
local function GetCharacters()
for _, player in pairs(Players:GetPlayers()) do
local char = player.Character or player.CharacterAdded:Wait()
if table.find(chars, char) then continue end
table.insert(chars, char)
end
end
GetCharacters()
Players.PlayerAdded:Connect(GetCharacters)
I didn’t think of that, But yeah this one is the way to go as it’s efficient.
The only reason i’m checking for characters in workspace because i’m making a “finish” system where when an event is fired it checks if the person who fired the event is near a downed person or not (downed person has a value insider their character model)
I tried your script however it for some reasaon printed multiple times instead of one.

this is what i did
for _, player in pairs(Players:GetPlayers()) do
print("Found Players")
local char = player.Character
print("Found char")
local torso = char:FindFirstChild("Torso")
print("found torso")
if torso and char:FindFirstChild("Values").Downed == true then
print("found distance and doing dmg")
The reason might be that there are two or more players. If there is indeed only one player then this shouldn’t be happening. Check the rest of the script to see if there is some kind of loop that makes the block of code run multiple times.
If it still doesn’t work, you can make it so it doesn’t run again for the same player by doing so:
local done = {} -- table containing the player's that have been run through in the loop
for _, player in pairs(Players:GetPlayers()) do
if table.find(done, player.Name) then continue end -- If the player is already in the table then skips to the next iteration of the for i, v loop
table.insert(done, player.Name) -- puts the player in the table to prevent running more than once
-- Now do the rest of stuff here
print("Found Players")
local char = player.Character
print("Found char")
local torso = char:FindFirstChild("Torso")
print("found torso")
if torso and char:FindFirstChild("Values").Downed == true then
print("found distance and doing dmg")
end
end)
Feel free to ask anything else.
I’m sorry for the late reply. I was very busy sleeping 