How do i make an npc look like a random player in the server

I have this killer in my game that spawns and i want it to be able to choose a random player in the server and become that player but how would i do that? specifically how do i make the script get a random player in the server?

Players | Roblox Creator Documentation This might help you

Thats something i know but my main question is how do i make the script get a random player from the server first?

those might help:

local Players = game:GetService("Players")

function getRandomPlayer()
	local players = Players:GetPlayers() 
	return players[math.random(1, #players)]

function applyOutfit(dummy, player)
	local description = Players:GetHumanoidDescriptionFromUserId(player.UserId) 
	local Success = pcall(function()
		local humanoid = dummy:FindFirstChildWhichIsA("Humanoid") 
	return Success 

local player = getRandomPlayer() 
applyOutfit(workspace.Dummy, player)

could i just try and set the dummy as script.parent instead, so i can have the script inside the NPC?

yes as long it’s a server script it will replicate to other users.

it is telling me this error:
Workspace.NPC.Script:6: invalid argument #2 to ‘random’ (interval is empty)

You should run that part of the code when the players aren’t 0.(If you plan to make a round-based game you should have a condition for that)

but i join the game don’t I, or is it erroring because there is 0 players and it runs before i join?

Correct. Although if you need a temporary fix just do

if #players > 0 then --are players in-game?
	return players[math.random(1, #players)] --chose one

It will return nil if no player is found

should i make that a looping statement or what because i have it in the getrandomplayer function

i feel that if i put it in the function and it returns nil then the script wont work, or should i make the function only work if playeradded

just add

repeat task.wait(.1) until #Players:GetPlayers() > 0

above your code(before calling the function)
that way the script will run after the first player joins

yeah thats what i did, but because my game is a round based game, i wouldn’t need it right?

That depends on how your script works. You can find out if it waits for players or no by doing


where you want to check(if it prints an empty array it isn’t waiting)

the script simply is going to run once the parent of the npc is workspace since it will be stored in SSS and by that point there will be at least one player unless the script just so happens to run right at the time the last player leaves

is it possible to make that a local script and have it still work?