Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by
Rdumb1 4
5 years ago

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.

0
Though sometimes the npcs have the same skin color. Rdumb1 4 — 5y
0
I would do that but i have just fixed it, thanks anyways FlabbyBoiii 81 — 5y
Ad
Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago

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
0
What? DeceptiveCaster 3761 — 5y
0
Having a space or not literally doesn't matter. DeceptiveCaster 3761 — 5y
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 SerpentineKing 3885 — 5y
1
THANKYOU!! :D, it worked a good FlabbyBoiii 81 — 5y
0
yeah LOL also you dont "call" a variable. Variables are just containers of arbitrary data User#24403 69 — 5y

Answer this question