Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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!

2 comments

0

Works! Thank you very much

0

No problem!

This discussion is preserved as it was. New answers, comments, and votes are closed.