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
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
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:
if ChosenPlayerCharacter then
ChosenPlayerCharacter.HumanoidRootPart.CFrame = game.Workspace:WaitForChild('CoolPart').CFrame
else
warn('Couldnt find Character for: '..ChosenPlayer
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.
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?
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!
Thank you so much for helping out, I really appreciate it