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
01 | script.Parent.MouseButton 1 Click:connect( function () |
02 |
03 | local w = game.Workspace:GetChildren() |
04 | for i = 1 , #w do |
05 | local c = w [ i ] :GetChildren() |
06 | for i = 1 , #c do |
07 | if (c [ i ] .className = = "Accessory" ) then |
08 | c [ i ] :remove() |
09 | end |
10 | end |
11 | end |
12 |
13 | 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:
1 | local Player = game:GetService( "Players" ).LocalPlayer |
2 |
3 | script.Parent.MouseButton 1 Click:Connect( function () |
4 | for _,characterAsset in pairs (Player.Character:GetChildren()) do |
5 | if (characterAsset:IsA( "Accessory" )) then |
6 | characterAsset.Handle:Destroy() |
7 | end |
8 | end |
9 | end ) |
Hope this helped, if so, feel free to accept my answer!