Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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
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) SideProjekt 39 — 4y

1 answer

Log in to vote
0
Answered by
palav 104
4 years ago

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.

Ad

Answer this question