I want to make a player wear an accessory by eliminating the ones already worn (on the head)

  1. What do i want to achieve? I want to make a player wear an accessory by eliminating the ones already worn (on the head)

  2. What is the issue? Basically when this local script run, it fires server. The accessory is worn by only one player, but not by everyone

--CLIENT

for i,v in pairs(game.Players.LocalPlayer.Character:GetChildren()) do
				if v:IsA("Accessory") then	
					if v.Handle:FindFirstChild("HatAttachment") or v.Handle:FindFirstChild("FaceFrontAttachment") then --FaceFrontAttachment
						v:Destroy() --destroy the head accessories
						local hum = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
						if hum then
							game.ReplicatedStorage.addastronauthelmelt:FireServer(game.Players.LocalPlayer)		
						end
					end
				end
			end
--SERVER

game.ReplicatedStorage.addastronauthelmelt.OnServerEvent:Connect(function(plr)	
	plr.Character.Humanoid:AddAccessory(game.ReplicatedStorage["Space Hat"]:Clone())
end)

Hi,

What you can do is put the accessory part into the ServerSide script. This is probably what it would look like now:

CLIENT:

(all you need to do in client is to fire the event!)

SERVER:

--SERVER

game.ReplicatedStorage.addastronauthelmelt.OnServerEvent:Connect(function(plr)	
        for i,v in pairs(plr.Character:GetChildren()) do
				if v:IsA("Accessory") then	
					if v.Handle:FindFirstChild("HatAttachment") or v.Handle:FindFirstChild("FaceFrontAttachment") then --FaceFrontAttachment
						v:Destroy() --destroy the head accessories
					end
				end
		end
	plr.Character.Humanoid:AddAccessory(game.ReplicatedStorage["Space Hat"]:Clone())
end)

Also, in your current code, it is possible that you are firing the event multiple times in Local Script, as you are running through a loop of the player’s accessories.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.