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

How do I revert a players costume to default?

Asked by 4 years ago

I have had some issues with coding a costume reverting block, an invisible part that you walk through.

when entering a room through one way, it puts a hazmat suit on the player that enters using the codes below:

Colour

function onTouch(hit) 
local human = hit.Parent:findFirstChild("Humanoid") 

if human ~= nil then 
    if human.RigType == Enum.HumanoidRigType.R15 then
        print("Rig R15") 
        hit.Parent:findFirstChild("LeftFoot").BrickColor = BrickColor.new(106)
        hit.Parent:findFirstChild("LeftHand").BrickColor = BrickColor.new(106)
        hit.Parent:findFirstChild("LeftLowerArm").BrickColor = BrickColor.new(106)
        hit.Parent:findFirstChild("LeftLowerLeg").BrickColor = BrickColor.new(106)
        hit.Parent:findFirstChild("LeftUpperArm").BrickColor = BrickColor.new(106)
        hit.Parent:findFirstChild("LeftUpperLeg").BrickColor = BrickColor.new(106)
        hit.Parent:findFirstChild("LowerTorso").BrickColor = BrickColor.new(106)
        hit.Parent:findFirstChild("RightFoot").BrickColor = BrickColor.new(106)
        hit.Parent:findFirstChild("RightHand").BrickColor = BrickColor.new(106)
        hit.Parent:findFirstChild("RightLowerArm").BrickColor = BrickColor.new(106)
        hit.Parent:findFirstChild("RightLowerLeg").BrickColor = BrickColor.new(106)
        hit.Parent:findFirstChild("RightUpperArm").BrickColor = BrickColor.new(106)
        hit.Parent:findFirstChild("RightUpperLeg").BrickColor = BrickColor.new(106)
        hit.Parent:findFirstChild("UpperTorso").BrickColor = BrickColor.new(106)
        hit.Parent:findFirstChild("HumanoidRootPart").BrickColor = BrickColor.new(106)
    else
        if human.RigType == Enum.HumanoidRigType.R6 then
            print ("Rig R6")
            hit.Parent:findFirstChild("Torso").BrickColor = BrickColor.new(106)
            hit.Parent:findFirstChild("Left Arm").BrickColor = BrickColor.new(106)
            hit.Parent:findFirstChild("Right Arm").BrickColor = BrickColor.new(106)
            hit.Parent:findFirstChild("Left Leg").BrickColor = BrickColor.new(106)
            hit.Parent:findFirstChild("Right Leg").BrickColor = BrickColor.new(106)
        end
    end
end 
hit.BrickColor = BrickColor.new(106) --this is for bricks
end 
script.Parent.Touched:connect(onTouch) 

--COLORS

--1 = white
--208 = Light stone grey
--194 = Medium stone grey
--199 = Dark stone grey
--26 = Black
--21 = Bright red
--24 = Bright yellow
--226 = Cool yellow
--23 = Bright blue
--107 = Bright bluish green
--102 = Medium blue
--11 = Pastel blue
--45 = Light blue
--135 = Sand blue
--106 = Bright orange
--105 = Br. yellowish orange
--141 = Earth green
--28 = Dark green
--37 = Bright green
--119 = Br. yellowish green
--29 = Medium green
--151 = Sand green
--38 = Dark orange
--192 = Reddish brown
--104 = Bright violet
--9 = Light reddish violet
--101 = Medium red
--5 = Brick Yellow
--153 = Sand red
--217 = Brown
--18 = Nougat
--125 = Light orange

Hat Remover

function onTouched(hit) 
    local d = hit.Parent:GetChildren() 
    for i=1, #d do 
        if (d[i].className == "Accessory") then 
            d[i]:Destroy()
            print("accesories removed") 
        end 
    end
end 

script.Parent.Touched:connect(onTouched) 
wait(3)

Accessory Giver

local Players = game:GetService("Players")
local button = script.Parent
local db = true

local function onTouch(hit)
    local human = hit.Parent:findFirstChild("Humanoid")
    local player = Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        if player.Character:FindFirstChild("Humanoid") then
            print(player)
            wait()
            db = false
            local hood = Instance.new("Accessory")
            hood.Name = "hood"
            local handle = Instance.new("Part")
            handle.Name = "Handle"
            handle.Size = Vector3.new(1,1.6,1)
            handle.Parent = hood
            local hatAttachment = Instance.new("Attachment")
            hatAttachment.Name = "HatAttachment"
            hatAttachment.Position = Vector3.new(0,0.75,0)
            hatAttachment.Parent = handle
            local mesh = Instance.new("SpecialMesh")
            mesh.Name = "Mesh"
            mesh.Scale = Vector3.new(1.1,1.1,1.1)
            mesh.MeshId = "rbxassetid://4746021596"
            mesh.TextureId = "rbxassetid://4742779269"
            mesh.Parent = handle
            player.Character.Humanoid:AddAccessory(hood)
            print("hood added")
            --next item
            local mask = Instance.new("Accessory")
            mask.Name = "mask"
            local handle = Instance.new("Part")
            handle.Name = "Handle"
            handle.Size = Vector3.new(1,1.6,1)
            handle.Parent = mask
            local faceAttachment = Instance.new("Attachment")
            faceAttachment.Name = "faceAttachment"
            faceAttachment.Position = Vector3.new(0,1.75,1)
            faceAttachment.Parent = handle
            local mesh = Instance.new("SpecialMesh")
            mesh.Name = "Mesh"
            mesh.Scale = Vector3.new(1.1,1.1,1.1)
            if human.RigType == Enum.HumanoidRigType.R15 then
                mesh.Offset = Vector3.new(0, -0.625, -0.5)
            else
                if human.RigType == Enum.HumanoidRigType.R6 then
                    mesh.Offset = Vector3.new(0, -0.625, -0.6)
                end
            end
            mesh.MeshId = "rbxassetid://4646958613"
            mesh.TextureId = "rbxassetid://4708693107"
            mesh.Parent = handle
            player.Character.Humanoid:AddAccessory(mask)
            print("mask added")
            db = true
        else
        end
    end
end

button.Touched:Connect(onTouch)

Clothes Giver

db = false
Hazmat = script.Parent.Parent.HazmatSuit

function onTouched(hit)
    h = hit.Parent:findFirstChild("Humanoid")
    if h~=nil and db == false then
        db = true
        e = h.Parent:GetChildren()
        for i=1,#e do
            if e[i].className == "Shirt" or e[i].className == "Pants" or e[i].className == "ShirtGraphic" or e[i].className == "Accesory" then
                e[i]:destroy()
            end
        end
    end
    e = Hazmat:GetChildren()
    for i=1,#e do
        if e[i].className == "Shirt" or e[i].className == "Pants" or e[i].className == "ShirtGraphic" then
            e[i]:clone().Parent = h.Parent
        end
        db = false
    end
end
script.Parent.Touched:connect(onTouched)

All these codes work, but I included them as they may be of use in some way to solving the issue of removing them, when leaving the room through a different exit.

I have tried 3 main variations of code to achieve the resetting of the player's costume without re-spawning.

1 - this removes the hazmat suit, however the colours are not reverted and the clothing and accessories of the players do not return, plus when a new player joins the game, if any other player re-spawns then the will look like the player that joined the server most recently.

local Players = game:GetService("Players")
db = false
local function onPlayerAdded(player)
    id = player.CharacterAppearanceId
    print(id)


    function onTouched(hit)
        h = hit.Parent:findFirstChild("Humanoid")
        if h~=nil and db == false then
            db = true
            player:ClearCharacterAppearance()
            print("appearence cleared")
            player:loadCharacter(id)
            print("loaded")
            --player:LoadCharacter()
            db = false
        end
    end
    script.Parent.Touched:connect(onTouched)
end

for _, player in pairs(Players:GetPlayers()) do
    onPlayerAdded(player)
end
Players.PlayerAdded:Connect(onPlayerAdded)

2 (which I found on a similar article on this website, however this may be outdated) How to change character appearance without having them respawn? EDIT

--This will add the character model to folder name Characters in ServerStorage
game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        if game.ServerStorage.Characters:FindFirstChild(player.Name) == nil then
            character.Archivable = true
            wait(2)
            character:Clone().Parent = game.ServerStorage.Characters
        end
    end)
end)

--When player leaves game this will remove the character model so that game don't use too much space
game.Players.PlayerRemoving:Connect(function(player)
    local playerCharacter = game.ServerStorage.Characters:FindFirstChild(player.Name)

    if playerCharacter ~= nil then
        playerCharacter:Destroy()
    end
end)

--This function will get the character model remove current and change your player to that one and move that model cframe position to your position
function loadCharacterAppearance(player)
    local characterModel =   game.ServerStorage.Characters:FindFirstChild(player.Name):Clone()
    characterModel:SetPrimaryPartCFrame(player.Character.PrimaryPart.CFrame)

    player.Character:Destroy()
    player.Character = characterModel
    characterModel.Parent = game.Workspace
end

3 same as previous

--Use this instead of that custom function make sure to get the player instance.
--E.g local player = game.Players.PlaasBoer  That will get my player.

player:ClearCharacterAppearance()

--This function reset appearance when called.
--This function will add the player appearance just pass the player through the argument when using it.
function loadCharacterAppearance(player)
    if player.UserId then
        local theModel = game.Players:GetCharacterAppearanceAsync(player.UserId)
        for _, child in pairs(theModel:GetChildren()) do
            player:LoadCharacterAppearance(child)
        end
    end
end

I have tested this out on studio and from Roblox.com

If you have any advice on how to correct this, it would be much appreciated. Thanks

1 answer

Log in to vote
0
Answered by
Rinpix 639 Moderation Voter
4 years ago

You can use what's called a HumanoidDescription.

You can create a new instance of it when a player enters the room and save their clothing, accessories, etc. to the HumanoidDescription.

Whenever they exit the room, call the function :ApplyDescription() on their Humanoid and pass in the HumanoidDescription.

This not only lets you save their appearance into a single instance, but it circumvents the entire process of modifying all of the body parts, removing the accessories, etc. It cleans your code up a whole lot.

Ad

Answer this question