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

Clothes changing when GUI clicked won't work?

Asked by 5 years ago

The button works fine (I tested it) but the rest won't for some reason.

function leftClick()
    local function replaceClothes(player)
        local character = player.Character
        if character then 
            local shirt = character:FindFirstChildOfClass("Shirt")
            local pants = character:FindFirstChildOfClass("Pants")
            if not shirt then 
                shirt = Instance.new("Shirt", character)
            end 
            if not pants then 
                pants = Instance.new("Pants", character)
            end
            shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=83326831"
            pants.PantsTemplate = "http://www.roblox.com/asset/?id=10045638"
        end
    end
    end


script.Parent.MouseButton1Click:Connect(leftClick)

2 answers

Log in to vote
0
Answered by
NSMascot 113
5 years ago
Edited 5 years ago

The initial problem i see is that other players won't see the players new clothes, so you will need to use remote events to fix it.

1) Add a remote event to replicatedstorage, call it ClothesEvent

2) add a localscript to a textbutton / imagebutton, put this code into it

local event = game.ReplicatedStorage.ClothesEvent

script.Parent.MouseButton1Click:Connect(function()
    event:FireServer()
end)

The script above fires the event to the server when the player presses the button.

3) now add a serverscript in ServerScriptService, put this code into it.

local event = game.ReplicatedStorage.ClothesEvent

event.OnServerEvent:Connect(function(player)
    local descendants = player.Character:GetDescendants()
    for index, descendant in pairs(descendants) do
    if descendant:IsA("Clothing") then
        descendant:Destroy()
    end
    end
    local shirt = Instance.new("Shirt")
    shirt.Name = "Shirt"
    shirt.Parent = player.Character
    shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=83326831"
    local pants = Instance.new("Pants")
    pants.Name = "Pants"
    pants.Parent = player.Character
    pants.PantsTemplate = "http://www.roblox.com/asset/?id=10045638"
end)

The script above runs when it receives the event from the client, the script looks through all of the descendants using :GetDescendants, then if the descendant is a clothing then it removes it. Then the script makes new shirt and pants with the desired templates and puts them in the character.

I've tested all the code and it works at the time that it was made (2nd January 2020)

Ad
Log in to vote
0
Answered by 5 years ago

Is this the whole script? You created a function called replacedClothes but never do anything to trigger it

You should use local script if you are working with gui, and you can use game.Players.LocalPlayer to get the player with local script, but if you change it using local script, other people won't be able to see the change

so instead, you can change the clothing on the server side using remote event

Example :

LocalScript :

local RepliStorage = game:GetService("ReplicatedStorage")
local Remote = RepliStorage:WaitForChild("ChangeClothing")
--I made a RemoteEvent called "ChangeClothing" and put it inside replicated storage beforehand

function leftClick()
       Remote:FireServer()
end


script.Parent.MouseButton1Click:Connect(leftClick)

Server side script (Preferably inside ServerScriptService)

local RepliStorage = game:GetService("ReplicatedStorage")
local Remote = RepliStorage:WaitForChild("ChangeClothing") 

Remote.OnServerEvent:connect(function(ply)
    local character = workspace:FindFirstChild(ply.Name)
        if character then 
            local shirt = character:FindFirstChildOfClass("Shirt")
            local pants = character:FindFirstChildOfClass("Pants")
                if not shirt then 
                    shirt = Instance.new("Shirt", character)
                end 
                if not pants then 
                    pants = Instance.new("Pants", character)
                end
                shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=83326831"
                pants.PantsTemplate = "http://www.roblox.com/asset/?id=10045638"
          end
end)
0
I've made a morph button and it didn't need a RemoteEvent. User#29913 36 — 5y
0
ok, and? Azure_Kite 885 — 5y

Answer this question