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

How to spawn npc with random clothing ?

Hello, a few weeks ago I got stuck on my code, the code only works if you put an id, except if you add 2 or more id the code when the npc appears becomes null..

shirts = {
	1118202405, 
	1537761670, 
	1442388484, 
	4413177915, 
	1471488077, 
	751482380, 
	2403967461
	
}

    local randomshirt = math.random(7,#shirts)
    local clothing = script.Parent.Clothing

clothing.ShirtTemplate = "rbxassetid://"..shirts[randomshirt]

2 answers

The only problem that I can see is that you're using math.random incorrectly (at least in this use case). math.random has two arguments; the minimum value and the maximum value. In this case you'd want the minimum value to be 1, not 7.

I'd also suggest setting the randomseed to get more random values. The most convenient way to truly randomize the seed is to just set it to tick(), which is a function that returns the time passed in Roblox's servers.

math.randomseed(tick())
shirts = {
    1118202405, 
    1537761670, 
    1442388484, 
    4413177915, 
    1471488077, 
    751482380, 
    2403967461

}

    local randomshirt = math.random(1,#shirts)
    local clothing = script.Parent.Clothing

clothing.ShirtTemplate = "rbxassetid://"..shirts[randomshirt]

2 comments

0

it works, except that the npc all have the same id put in ShirtTemplate, except that the game refuses to display them in the npc directly, the data is well put, but no display update is done on the npc.

0

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