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

Why can I not add hats to this script. It always gives me a nil value?[Solved]

Asked by 6 years ago
Edited 6 years ago

I have a script and it adds shirts and and pants to the character, along with a weapon. But everytime I try to add a hat or a accessory it says it not part of folder, even know it is.

Heres the script, if anyone could help that would be great :)

local classHandler = game.ServerStorage.ClassHandler
local clothingStorage = game.Workspace.ClothingStorage
local changeOutfit = game.ReplicatedStorage.ChangeOutfit

function setClass(class, player)
    local char = player.Character

    -- Makes your class Santa
    if class == "Santa" then
        local folder = clothingStorage.Santa 

        if char:findFirstChild("Shirt") then
            char.Shirt:Destroy()

        elseif char:findFirstChild("Pants") then
            char.Shirt:Destroy()

        elseif  char:FindFirstChild("Accessory") then
            char.Accessory:Destroy()
        end

        folder.Shirt:clone().Parent = char
        folder.Pants:clone().Parent = char

        local weapon = game.ReplicatedStorage.Thompson
        weapon:clone().Parent = player.Backpack

    -- Makes your class Snowman
    elseif class == "Snowman" then
        local folder = clothingStorage.Snowman

        if char:findFirstChild("Shirt") then
            char.Shirt:Destroy()

        elseif char:findFirstChild("Pants") then
            char.Shirt:Destroy()

        elseif  char:FindFirstChild("Accessory") then
            char.Accessory:Destroy()
        end

        folder.Shirt:clone().Parent = char
        folder.Pants:clone().Parent = char

        local weapon = game.ReplicatedStorage.PumpShotgun
        weapon:clone().Parent = player.Backpack

    end
end

function changeOutfit.OnServerInvoke(player, class)
    local value = classHandler:findFirstChild("Player_"..player.UserId)

    if value then
       value.Value = class
    end

    setClass(class, player)
end

game.Players.PlayerAdded:connect(function(player)

    -- This stores which class the player has currently selected
    local value = Instance.new("StringValue")
    value.Name = "Player_"..player.UserId
    value.Parent = classHandler

    player.CharacterAdded:connect(function(char)
        setClass(value.Value, player)
    end)
end)

game.Players.PlayerRemoving:connect(function(player)
    local value = classHandler:findFirstChild("Player_"..player.UserId)

    if value then
       value:Destroy()
    end
end)

Answer this question