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

Accessory giver script giving player tons of accessories. Why?

Asked by 2 years ago

Hello! I am trying to make a script where the player can click on a part to give them accessories. I have a folder of accessories in serverstorage. Here's the whole script below.

script.Parent.MouseClick:Connect(function(player)
    local char = player.Character
    local hum = char:WaitForChild("Humanoid")
    local hats = char:GetChildren()

    for b = 1, #hats do
        if hats[b].ClassName == ("Accessory") then
            hats[b]:Destroy()

            local accessories = game.ServerStorage.Accessories.Captain:GetChildren()
            if char:FindFirstChild("Shirt") and char:FindFirstChild("Pants") then
                char.Shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=107043249"
                char.Pants.PantsTemplate = "http://www.roblox.com/asset/?id=107043215"

                for i = 1, #accessories do

                    if accessories[i]:IsA("Accessory") then
                        accessories[i]:Clone().Parent = char
                    else

                            accessories[i]:Clone().Parent = char

                        end
                    end
                end
            end
        end
    end)

For some reason, when I get rid of lines 6, 7, and 8 the script works perfectly fine. If anybody could help, that would be great.

0
i think thats because of the for loop Puppynniko 1059 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

I think that you are trying to destroy any current hats on the player, and then add different ones. Your "end"s are just in the wrong place :)

script.Parent.MouseClick:Connect(function(player)
--Variables
    local char = player.Character
    local hum = char:WaitForChild("Humanoid")
    local hats = char:GetChildren()

--Destroy old hats
    for b = 1, #hats do
        if hats[b].ClassName == ("Accessory") then
        hats[b]:Destroy()
    end

--Change the character's shirt and pants
   if char:FindFirstChild("Shirt") and char:FindFirstChild("Pants") then
                char.Shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=107043249"
                char.Pants.PantsTemplate = "http://www.roblox.com/asset/?id=107043215"
   end

-- Clone all accessories from the Captain folder to the character
   local accessories = game.ServerStorage.Accessories.Captain:GetChildren()
   for i = 1, #accessories do
   if accessories[i]:IsA("Accessory") then
        accessories[i]:Clone().Parent = char
   end
end)

I hope this helps

~Amanda314159

Ad

Answer this question