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 4 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

01script.Parent.MouseButton1Click:connect(function()
02 
03local 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 
13end)

1 answer

Log in to vote
1
Answered by 4 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:

1local Player = game:GetService("Players").LocalPlayer
2 
3script.Parent.MouseButton1Click: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
9end)

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

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

Answer this question