I'm making some game that requires 20 or so NPCs to spawn with different skin colors and stuff (I have a single NPC in ReplicatedStorage)
The script i made is supposed to Clone the NPC Set the parent of the NPC to workspace and change it's skin color, so i used this script here
1 | for i = 1 , 20 do |
2 | local Color = game.Workspace.BodyTypes [ "Body" .. math.random( 1 , 7 ) ] :Clone() |
3 | local NPC = game.ReplicatedStorage.NPC:Clone() |
4 | Color.Parent = NPC |
5 | NPC.Parent = game.Workspace |
6 | end |
so i intended it to access the Folder (BodyTypes) and grab a random BodyColors out of it and i did that with a math.Random so if it chose 1 it would be Body1 if it chose 2 it would be Body2 etc. but when i test the game the math.Random chooses the same body color every time apart from the first clone, so all the npcs look the same, i don't know why this is happening, any help? (Also the NPC goes into the workspace and BodyColors goes into the NPC, but they all end up the same apart from 1)
You know could just put this script inside the npc right?
1 | colors = { "Daisy orange" , "Deep orange" } |
2 | Color = BrickColor.new(colors [ math.random( 1 , 2 ) ] ) --Change 2 if you want more body colors |
3 | for i,v in pairs (script.Parent:GetChildren()) do |
4 | if v.Name = = 'Head' or v.Name = = 'Right Arm' or v.Name = = 'Left Arm' or v.Name = = 'Torso' then |
5 | v.BrickColor = Color |
6 | end |
7 | end |
It works fine. However, if you put more than 2, you have to change 2 on math.random.
ISSUE
When using the ".." in the string, there should not be a space between this and the defined variable.
GENERAL PRACTICE
Use :WaitForChild()
to check that an item exists before using it in a function
The For-Loop has an increment parameter you can set
To properly ascertain the ReplicatedStorage use :GetService()
method
You can index the Workspace using workspace
instead of game.Workspace
REVISED Server Script
1 | for i = 1 , 20 , 1 do |
2 | local random = math.random( 1 , 7 ) |
3 | local Color = workspace:WaitForChild( "BodyTypes" ):WaitForChild( "Body" ..random):Clone() |
4 | local NPC = game:GetService( "ReplicatedStorage" ):WaitForChild( "NPC" ):Clone() |
5 | Color.Parent = NPC |
6 | NPC.Parent = workspace |
7 | end |