What do you want to achieve?
So, basically, I want to make the random appearance system for NPCs, and, make sure all accessories are certain. Like, what I meant is, everytime you make a new NPC, it will pick some random accessories, such as hair, hat, glasses, masks and more for them to wear. (Depends on their gender.)
What is the issue?
I tried to make the one myself, but, I feel so lost, because, I’m not sure what I’m doing with it, and, I’ve had a few problems with it. Any advices would be appreciated.
What solutions have you tried so far?
I’ve tried to look everywhere, and, unfortunately, there doesn’t seem to be such a thing like that. So, I’m hoping anyone here knows how to do this.
Even if I should just make a bunch of NPCs with the different appearance in the folder, for me personally, I’d prefer not to do that, for some reasons.
Yeah, that’s what I’m basically doing, but, the thing is, is there a way to prevent NPC from wearing the wrong accessories or something like this?
Like, for example, let’s just say, if you want to make a soldier with beret only or whatever, but, they have another accessories in the same order, when that’s not supposed to happen.
Or, if you just want to make a woman, but, she has a facial hair that only men wears. Is there any way to prevent that happening?
Then, adding on to his script, you would have to have a separate folder for the genders and maybe an attribute in the NPCModel that gets set in a randomizer (Like maybe 1 == boy and 2 == girl, or just as strings).
-- Just an example ServerScript, located in the model of the NPC.
local ReplicationStorage = game:GetService("ReplicationStorage")
local AccessoriesFolder = ReplicationStorage:WaitForChild("Accessories")
local SetAccessoryFolder -- we'll set this later, for now it's nil.
local NPC = script.Parent -- assuming the scripts parented to the NPC.
NPC:SetAttribute("Gender", math.random(1, 2)) -- setting the gender via number.
local Humanoid = NPC.Humanoid
local Gender = NPC:GetAttribute("Gender") -- it'll return as a number.
-- using attributes, it allows other scripts to get or modify this data.
-- if you're not planning to use this universally through other scripts,
-- you can just do:
-- local Gender = math.random(1, 2)
local howManyHats = math.random(1, 3) -- just randomizing the amount of
-- hats the NPC can be able to wear.
if Gender == 1 then
SetAccessoryFolder = AccessoriesFolder.Woman
-- assuming it's a folder parented to the AccessoriesFolder
else
SetAccessoryFolder = AccessoriesFolder.Man
-- assuming it's a folder parented to the AccessoriesFolder
end
local AccessoryTable = SetAccessoryFolder:GetChildren()
local obtainableHats = #AccessoryTable -- how many hats that is useable
for count = 1, howManyHats do -- a for loop to acquire hats.
local newHat = AccessoryTable[math.random(1, obtainableHats)]:Clone()
newHat.Parent = NPC -- assuming it's class is an Accessory or Hat.
-- alternatively, you could just do this:
-- local newHat = AccessoryTable[math.random(1, obtainableHats)]
-- Humanoid:AddAccessory(newHat)
end
Okay, I’ll try it out and see if It works. Also, is it possible to add the whitelist and blacklist on it? Like, I meant, I don’t want a NPC to wear the beret and raised goggles at the same time, or, a civilian with the floating helmet addons. That’s what worries me the most.
Yes! You could then modify the NPC to have a randomized attribute/value that expressed what type of NPC it is and what it should wear.
Here’s the new code with what you wanted:
-- Just an example ServerScript, located in the model of the NPC.
local ReplicationStorage = game:GetService("ReplicationStorage")
local AccessoriesFolder = ReplicationStorage:WaitForChild("Accessories")
local SetAccessoryFolder -- we'll set this later, for now it's nil.
local NPCType = {
"Civilian",
"Military",
"Officer" -- you can add as many as you'd like.
}
local NPC = script.Parent -- assuming the scripts parented to the NPC.
NPC:SetAttribute("Gender", math.random(1, 2)) -- setting the gender via number.
local Humanoid = NPC.Humanoid
local Gender = NPC:GetAttribute("Gender") -- it'll return as a number.
-- using attributes, it allows other scripts to get or modify this data.
-- if you're not planning to use this universally through other scripts,
-- you can just do:
-- local Gender = math.random(1, 2)
local howManyHats = math.random(1, 3) -- just randomizing the amount of
-- hats the NPC can be able to wear.
if Gender == 1 then
SetAccessoryFolder = AccessoriesFolder.Woman
else
SetAccessoryFolder = AccessoriesFolder.Man
end
-- local AccessoryTable = SetAccessoryFolder:GetChildren()
-- local obtainableHats = #AccessoryTable -- how many hats that is useable
-- we won't be needing those now...
-- instead, we'll use this:
local TypeOfNPC = NPCType[math.random(1, #NPCType)]
for count = 1, howManyHats do -- a for loop to acquire hats.
local HatsFor -- this is to express the type of hats the NPC can wear.
for _, class in ipairs(NPCType) do -- making it easier with a for loop.
if TypeOfNPC:lower() == class:lower() then -- this allows it to NOT
-- be case sensitive.
HatsFor = SetAccessoryFolder[class] -- assuming the class
-- is a folder parented to the AccessoryFolder.
end
end
local newHat = HatsFor[math.random(1, #HatsFor)]:Clone()
newHat.Parent = NPC -- assuming it's an accessory
end
You could have the accessories have the same Attribute that changes the type of hat it is. But it would cause not only a headache, but also it wouldn’t be good randomizing. So, having the accessories in a folder with the class name would be better and not as difficult.
I mean, that’d be too much work, especially if you have too many accessories here, but, I guess, I’ll give it a try. If not working, I’ll try to figure it out.
Yeah you can do something like this. Also to make it better, use UserID instead of names.
local WL = {"KultDeeri"}
local BL = {"Player1"}
if table.find(WL, plr.Name) then
--whitelisted
elseif table.find(BL, plr.Name) then
--blacklisted
return
end
end
The problem is that, I want to use the Dictionaries for each accessories with whitelist and blacklist in it, but, It couldn’t find any one, and, I don’t know what I’m doing wrong.