Having Issues with getting a Players Character from a Table

  • I’ve made this simple script where I’m taking all the players in the game in a table, then it will choose a random player from that table and set their position to a part I made in the Workspace. However, it doesn’t change the characters CFrame to the part. Everything else seems to work, and it prints out as it should in the output.

Most of this works fine, however, it doesn’t change the characters CFrame to the part. Everything else seems to work, and it prints out as it should in the output.

  • I’ve tried looking through Developer Forum posts, read about the ‘‘Players’’ Service and Table. I’ve also read a document about protected calls, which is what I’m using to get the random player
-- // variables // --
local WaitTimer = script:GetAttribute('WaitTimer') or 60 -- 60 seconds as default

-- // Setting the players in the table, and actually making the table // --

local PlayerTable = {}

while true do
    for _, player in ipairs(game.Players:GetPlayers()) do
        table.insert(PlayerTable, player.Name)
    end

    -- // choosing a random player from that table // --

    success, result = pcall(function()

        if #PlayerTable > 0 then 
            local ChosenPlayer = PlayerTable[math.random(1, #PlayerTable)] 

            warn('The Chosen Player is: '..ChosenPlayer)

            local ChosenPlayerCharacter = ChosenPlayer:WaitForChild('Character') 
           
            if ChosenPlayerCharacter then
                ChosenPlayerCharacter.CFrame = game.Workspace:WaitForChild('CoolPart').CFrame
            else
                warn('Couldnt find Character for: '..ChosenPlayer)
                
            end

        else
            warn('Error: Table is empty')
        end

    end) 

    -- // waiting for next loop // --

    task.wait(WaitTimer)
end

I’m simply looking for a person that can help me understand why this piece of code isn’t working, since I just can’t seem to grasp the solution to this. Any help is highly appriciated

1 Like

Have you tried using the CFrame of the characters HumanoidRootPart because doing it with models won’t work.

Something like this:

local WaitTimer = script:GetAttribute('WaitTimer') or 60 -- 60 seconds as default

-- // Setting the players in the table, and actually making the table // --

local PlayerTable = {}

while true do
    for _, player in ipairs(game.Players:GetPlayers()) do
        table.insert(PlayerTable, player.Name)
    end

    -- // choosing a random player from that table // --

    success, result = pcall(function()

        if #PlayerTable > 0 then 
            local ChosenPlayer = PlayerTable[math.random(1, #PlayerTable)] 

            warn('The Chosen Player is: '..ChosenPlayer)

            local ChosenPlayerCharacter = ChosenPlayer:WaitForChild('Character') 
           
            if ChosenPlayerCharacter then
                ChosenPlayerCharacter.HumanoidRootPart.CFrame = game.Workspace:WaitForChild('CoolPart').CFrame
            else
                warn('Couldnt find Character for: '..ChosenPlayer)
                
            end

        else
            warn('Error: Table is empty')
        end

    end) 

    -- // waiting for next loop // --

    task.wait(WaitTimer)
end

You are unable to retrieve the player character from using :WaitForChild(), instead you have to reference it like ChosenPlayer.Character!

Oh right, completely forgot about that Haha

  • I implemented it into the code, but it still doesn’t seem to ‘‘teleport’’ the player to the Parts CFrame. Could it be because it’s having a hard time getting the player’s character you reckon?

No errors in output either, only prints out what I’ve told it to, but once I add a ‘‘debug’’ print e.g:

print(ChosenPlayerCharacter)

nothing seems to print out…

1 Like

Are you doing this on a serverscript or localscript?

Changed this and now it prints this out:

Which is at:

 if ChosenPlayerCharacter then
                ChosenPlayerCharacter.HumanoidRootPart.CFrame = game.Workspace:WaitForChild('CoolPart').CFrame
            else
                warn('Couldnt find Character for: '..ChosenPlayer

Server Script located in ServerScriptStorage :smile:

No wonder why it doesn’t work cause you are inserting the name instead of the player, which instead should be:

for _, player in ipairs(game.Players:GetPlayers()) do
        table.insert(PlayerTable, player)
end

WIth storing the Player.Name, you’re going to need to find it in the player list or just reference the player itself!

I edited a bit of the code:

local WaitTimer = script:GetAttribute('WaitTimer') or 60 -- 60 seconds as default

-- // Setting the players in the table, and actually making the table // --

local PlayerTable = {}

while true do
    for _, player in ipairs(game.Players:GetPlayers()) do
        table.insert(PlayerTable, player.Name)
    end

    -- // choosing a random player from that table // --

    success, result = pcall(function()

        if #PlayerTable > 0 then 
            local ChosenPlayer = PlayerTable[math.random(1, #PlayerTable)] 

            warn('The Chosen Player is: '..ChosenPlayer)

            local ChosenPlayerCharacter = game.Workspace:FindFirstChild(ChosenPlayer)
           
            if ChosenPlayerCharacter then
                ChosenPlayerCharacter.HumanoidRootPart.CFrame = game.Workspace:WaitForChild('CoolPart').CFrame
            else
                warn('Couldnt find Character for: '..ChosenPlayer)
                
            end

        else
            warn('Error: Table is empty')
        end

    end) 

    -- // waiting for next loop // --

    task.wait(WaitTimer)
end

I made the ChosenPlayerCharacter variable check for the Chosen player’s name in Workspace which will find their character. This should work now.

1 Like

Ah, right!

  • Tried this but it still doesn’t seem to change the HumanoidRootPart’s CFrame to the Part, and it doesn’t print out (warn) anywhere in the script either?

This is the only print I’m receiving, because my player didn’t get time to load in, the table is empty. However after waiting the Wait() it prints out the rest, yet doesn’t now?

Cause that part should just be:

local ChosenPlayerCharacter = ChosenPlayer.Character

Try using game.Players:GetChildren() just to test!

That really doesn’t change anything, in my view.

My thought process is the issue is with the table insertion, so maybe something is going wrong with looping through the player instances.

Thank you! This worked, however I’m a bit confused as of to ‘‘why’’ this worked, haha.

When getting the ‘‘ChosenPlayer’’, i though it was getting the player from the ‘‘game.Players’’ service, not from the workspace?

It seems that in the code you provided you’re checking for the ‘‘ChosenPlayer’’ in the workspace at:

local ChosenPlayerCharacter = game.Workspace:FindFirstChild(ChosenPlayer)

Is this because the ChosenPlayer put into the table is actually the Character of the player? Sorry for my confusing questions :joy:

1 Like

There isn’t, the op just inserted the players the wrong way (inserting name instead of player) and the way he got the character is also wrong, if he fixes that, this topic should be resolved by now.

The ChosenPlayer variable is the name of the player that was chosen. What i did was change the ChosenPlayerCharacter variable to find the players name in Workspace which will find their character.

Ohh right, so you’re just going through Workspace until you see a Model that shares a name with the ‘‘ChosenPlayer’’ variable, then setting the ChosenPlayerCharacter to that, makes sense! :smile:

Thank you so much for helping out, I really appreciate it :smile:

Also thank you! @FerbZides and @loganmaker3005

3 Likes

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