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
for i = 1,20 do local Color = game.Workspace.BodyTypes["Body".. math.random(1,7)]:Clone() local NPC = game.ReplicatedStorage.NPC:Clone() Color.Parent = NPC NPC.Parent = game.Workspace 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?
colors={"Daisy orange", "Deep orange"} Color=BrickColor.new(colors[math.random(1,2)]) --Change 2 if you want more body colors for i,v in pairs(script.Parent:GetChildren()) do if v.Name=='Head' or v.Name=='Right Arm' or v.Name=='Left Arm' or v.Name=='Torso' then v.BrickColor=Color end 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
for i = 1, 20, 1 do local random = math.random(1, 7) local Color = workspace:WaitForChild("BodyTypes"):WaitForChild("Body"..random):Clone() local NPC = game:GetService("ReplicatedStorage"):WaitForChild("NPC"):Clone() Color.Parent = NPC NPC.Parent = workspace end