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

Script which removing hats, removing hats for everyone, how can I fix this?

Asked by 3 years ago

Hello! I have a script which removing hats, but when you click it, script removing hat for everyone. My script is in local script, I had it first in normal script but it works worse. One more thing, script works in GUI in textbutton. How can I fix this?

This is this script

script.Parent.MouseButton1Click:connect(function()

local w = game.Workspace:GetChildren() 
    for i = 1, #w do 
    local c = w[i]:GetChildren()
    for i=1, #c do
    if (c[i].className == "Accessory") then 
    c[i]:remove() 
    end
    end
    end

end)

1 answer

Log in to vote
1
Answered by 3 years ago

Hello!

I have analysed your script, and the reason why hats are being removed for everybody is because you are accessing the player from Workspace, which is where all Players' characters are located. A simple way to fix this is to use a LocalPlayer function to define the player. Here is a fixed version of your script, that will remove the hats only for the person who clicked the button:

local Player = game:GetService("Players").LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
    for _,characterAsset in pairs(Player.Character:GetChildren()) do
  if (characterAsset:IsA("Accessory")) then
      characterAsset.Handle:Destroy()
        end
    end
end)

Hope this helped, if so, feel free to accept my answer!

0
Works! Thank you very much ThadonROOX 47 — 3y
0
No problem! RazzyPlayz 497 — 3y
Ad

Answer this question