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

How can I get a part to spawn above a random player?

So I've been trying to get a part to spawn above a random player, and I've tried a bunch of different scripts with various errors, most recently getting a " randomPlayer is not a valid member of DataModel " error.

Here's my code, any advice would be greatly appreciated.

local randomPlayer = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]
while true do
wait(1)
part = script.Parent.Debris:clone()
part.Parent = script.Parent
part.Anchored = true
part.Position = game.randomPlayer.Character.Torso.Position
end

1 comment

0

Thank you so much, it took me a bit, but finally got it working (Was getting a lot of errors about the character not existing in Workspace)

1 answer

✓ Accepted answer

You didn't index the table the right way, plus you didn't get the character the right way. Below you can find a way to index the table correctly.

local PlayersTable = game.Players:GetPlayers()
local RandomPlayer = PlayersTable[math.random(1,#PlayersTable)]

To fix your player issue, you have to first understand that the Players part of the workspace only holds the clients. To get their actual characters, you have to find them from workspace. To do that, get the name of the player(which is whatever RandomPlayer is), and do

game.workspace:FindFirstChild(RandomPlayer)

Then, I'd probably just use a weld to make the part follow the player.

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