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

Insert Service Hat spawn not right?

Asked by 6 years ago

Basically, I've made a code to randomize skin color, hair, and face. Unfortunately, the hat that spawns doesnt automatically spawn on the player but far away. HOWEVER! The actual hat IS parented inside the character, and when I touch the visible hat, THEN it appears on the players head.

--Hats
local InsertService = game:GetService("InsertService")


if HatChance == 1 then
    local Load = InsertService:LoadAsset(TreckyHair)
    Load.Parent = player.Character
elseif HatChance == 2 then
    local Load = InsertService:LoadAsset(ChestnutStyle)
    Load.Parent = player.Character.Head
elseif HatChance == 3 then
    local Load = InsertService:LoadAsset(Shaggy)
    Load.Parent = player.Character.Head
elseif HatChance == 4 then
    local Load = InsertService:LoadAsset(StylishBrownHair)
    Load.Parent = player.Character.Head
elseif HatChance == 5 then
    local Load = InsertService:LoadAsset(ChestnutSpikes)
    Load.Parent = player.Character.Head
elseif HatChance == 6 then
    local Load = InsertService:LoadAsset(BlackPigtails)
    Load.Parent = player.Character.Head
elseif HatChance == 7 then
    local Load = InsertService:LoadAsset(BlondePigtails)
    Load.Parent = player.Character.Head
elseif HatChance == 8 then
    local Load = InsertService:LoadAsset(Beard1)
    Load.Parent = player.Character.Head
end

How can I make the Hat automatically spawn on the characters head?

1 answer

Log in to vote
0
Answered by
nilVector 812 Moderation Voter
6 years ago

When you Load() assets from InsertService, a Model will be returned, and inside that Model, the asset will be there. That's one of the reasons why it's working -- you're not parenting an accessory, you're parenting a model.

Secondly, you shouldn't be parenting Accessory objects directly to the head. You should be parenting them simply to the player's Character, and Roblox will take care of positioning the Accessory onto the Character itself accordingly.

Knowing all this, your inserting and parenting should look something like this:

local Load = InsertService:Load(assetId)
Load:FindFirstChildOfClass("Accessory").Parent = player.Character
0
Thanks so much! I have a small additional question though; having watched many Data Store tutorials, it's said that you can only save values. But in another game I saw, they were able to save your hat even if you left the game. How is that possible? Rebelbeard 2 — 6y
0
According to the Wiki (http://wiki.roblox.com/index.php?title=API:Class/GlobalDataStore/SetAsync), you can only save Variants (which are ints, strings, bools, and nil values). The hat saving you've seen is probably just saving of the assetId of the hat when the player leaves/loading of the assetId when the player joins (or texture too if it's a custom made hat not from the catalog). nilVector 812 — 6y
Ad

Answer this question