Can someone help me fix this script? [New to Scripting]

So I am a Scripter/Owner for a Napoleonic group and I am trying to make it so that when a player spawns in they spawn with a hat but if the player is for example rank 234 in my group and another player is rank 100 they would spawn with a different hat, this is the script I tried but it doesn’t work so if someone would help me point out the problem I would appreciate it.

local pModel = script.Parent
local hum = pModel:WaitForChild("Humanoid")
local hat = game.ServerStorage.Accessories.NerdHair:Clone()
local hat2 = game.ServerStorage.Accessories.FrenchHat1:Clone()
local Group = 5452911



game.Players.PlayerAdded:Connect(function(player)
	local character = nil
	if player:GetRankInGroup(Group) == 254 then
		hum:AddAccessory(hat)
		hum:AddAccessory(hat2)
	else
		if player:GetRankInGroup(Group) == 253 then
			hum:AddAccessory(hat)
		end
	end
end)

Why do you have pModel and hum defined outside the PlayerAdded event? That’s most likely your issue.

When the player joins, PlayerAdded is triggered, giving you the Player object which you can get the Character and Humanoid from.

local hat = game.ServerStorage.Accessories.NerdHair:Clone()
local hat2 = game.ServerStorage.Accessories.FrenchHat1:Clone()
local Group = 5452911

game.Players.PlayerAdded:Connect(function(player)
	local character = player.Character
	local hum = character.Humanoid
	if player:GetRankInGroup(Group) == 254 then
		hum:AddAccessory(hat)
		hum:AddAccessory(hat2)
	elseif player:GetRankInGroup(Group) == 253 then
		hum:AddAccessory(hat)
	end
end)
1 Like

Please edit your post, highlight your code, then click on the button above your edit window with the </> icon to format that text so we can read it a little easier.

What type of script is this, LocalScript or Script?
Where is the script located?

To change the type of hat for different ranks you can’t use == since that only gets that rank. Use <= or >= or a combination of them so you can have more than two ranges of ranks and hats.

I tried that but sadly it wasn’t the problem

There you go, is that better? It is a Script and it located in StarterCharacterScripts

Okay, I think I see the issue. Since you’re using StarterCharacterScripts, the script might be running before the ServerStorage has loaded. You should try to utilize a WaitForChild() on the variables for the hats.

local players = game:GetService("Players")
local hat = game.ServerStorage.Accessories.NerdHair:Clone()
local hat2 = game.ServerStorage.Accessories.FrenchHat1:Clone()
local Group = 5452911

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local hum = character:WaitForChild("Humanoid")
		if player:GetRankInGroup(Group) == 254 then
			hum:AddAccessory(hat)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) == 253 then
			hum:AddAccessory(hat)
		end
	end)
end)

Needs to be a server script since you’re referencing “ServerStorage” which can only be accessed from server scripts.

Sadly this didn’t solve the problem. I don’t think I made anything wrong with the accessory, I’ll double check it.

Perhaps an incorrect reference to the accessories. Make sure the accessories are Accessory instances and not some other class type.

local players = game:GetService("Players")
local hat = game.ServerStorage.Accessories.NerdHair
local hat2 = game.ServerStorage.Accessories.FrenchHat1
local Group = 5452911

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local hum = character:WaitForChild("Humanoid")
		if player:GetRankInGroup(Group) == 254 then
			local hatClone = hat:Clone()
			hum:AddAccessory(hatClone)
			local hat2Clone = hat2:Clone()
			hum:AddAccessory(hat2Clone)
		elseif player:GetRankInGroup(Group) == 253 then
			local hat2Clone = hat2:Clone()
			hum:AddAccessory(hat2Clone)
		end
	end)
end)

The above script is working for me, again make sure it isn’t a local script.

image

Ignore “Part” that’s just the baseplate, above is how everything is organised.

They’re all good, nothing wrong with the accessories, they have worked with other scripts

Check the above reply, this is working my end.

Proof:
image

Make sure those rank numbers are correct as well.

Ahh I found the problem, it was another script I made that would remove any accessories the player had on them when they removed, it most’ve removed them after the hats where given

Good chance, you should combine the two, remove hats first then apply the hats if in group with a certain rank after.

At least everything is now working.

Yeah thanks for the help dude, much appreciated

It is working although it is removing everything still anything I could do to fix that?

Did you fix the == issue?
Your script will only give players with rank 253 or 254.
If they are any other rank they won’t get a hat.

I know that which I will fix later on