Putting-a-hat-on-a-player

So I am trying to make it when a player clicks on a GUI box it equips a hat to them but only if they have the correct amount of pop points (like a rank) this is my code

Code

wantedhat = nil
script.Parent.MouseButton1Click:Connect(function()
if game.Players.LocalPlayer.leaderstats.PopPoints.Value >= 2 then

	local hum = character.Humanoid
	local ss = game:GetService("ReplicatedStorage")
	local hat = ss:WaitForChild("TopHat"):Clone()
	hum:AddAccessory(hat)

end
end)

1 Like

Try something like this real quick!

script.Parent.MouseButton1Click:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:wait()
	local hum = char.Humanoid
	local Replicated = game:GetService("ReplicatedStorage")
	local hat = Replicated:WaitForChild("TopHat"):Clone()
	if plr.leaderstats.PopPoints.Value >= 2 then
		hum:AddAccessory(hat)
	else
		print("PLAYER DOES NOT HAVE ENOUGH POINTS")
	end
end)
1 Like

I just get an error saying " Players.king_B99.PlayerGui.ScreenGui.Pop.TopHat.LocalScript:2: attempt to index nil with ‘Character’"

1 Like

Use that script one more time, but try and make it a LOCALSCRIPT.

Another way to fix :

Just change it so, the LOCALSCRIPT Fires an event to put the hat onto someones head!

locally doing add accessory will not work.

fire to the server and from there, do Humanoid:AddAccessory

also @TwinPlayzDev is right, you should be using a local script for all guis, but instead of adding accessory directly from local, you have to fire to server the parameter hat and then addaccessory

2 Likes