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)
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!