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

Npcs all have the same BodyColor even though they should all have a random one, why?

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) so i made a Folder in Workspace with the BodyColors in it (7 of them), so i can access them easily from my script

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)

2 answers

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.

2 comments

0

Though sometimes the npcs have the same skin color.

0

I would do that but i have just fixed it, thanks anyways

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

5 comments

0

What?

0

Having a space or not literally doesn't matter.

0

ok, well either way when you do it this way it works, so if that's not the issue then idk since I didn't receive any other errors during testing

1

THANKYOU!! :D, it worked a good

0

yeah LOL also you dont "call" a variable. Variables are just containers of arbitrary data

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