I need help with these 2 scripts [New to scripting] SOLVED

Alright so I have these 2 script which I need help with. Here’s the first one:

local players = game:GetService("Players")
local hat = game.ServerStorage.Accessories.RankerHat:Clone()
local hat2= game.ServerStorage.Accessories.NerdHair:Clone()
local hat3 = game.ServerStorage.Accessories.OfficerHat:Clone()
local Group = 12770278

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Wait()
	player.Character.Humanoid:RemoveAccessories()
end)

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local hum = character:WaitForChild("Humanoid")
		if player:GetRankInGroup(Group) == 254 then
			wait(3)
			hum:AddAccessory(hat3)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) == 253 then
			wait(3)
			hum:AddAccessory(hat3)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) == 10 then	
			wait(3)
			hum:AddAccessory(hat3)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) == 9 then
			wait(3)
			hum:AddAccessory(hat3)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) == 8 then
			wait(3)
			hum:AddAccessory(hat3)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) == 7 then
			wait(3)
			hum:AddAccessory(hat3)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) == 6 then	
			wait(3)
			hum:AddAccessory(hat3)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) <= 5 then
			wait(3)
			hum:AddAccessory(hat)
			hum:AddAccessory(hat2)
		end
	end)
end)

This script gives people in my group different hats but when they die/reset it disappears. If someone could explain why that happens I would appreciate it.

Here’s the second one:

local flintlock = game.ServerStorage.Weapons.Flintlock:Clone()
local flag = game.ServerStorage.Weapons.Colour:Clone()
local Group = 12770278


game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		if player.TeamColor == BrickColor.new("Maroon") then
			print("Onteam")
			if player:GetRankInGroup(Group) >= 5 then 
				flintlock.Parent = player.Backpack
			elseif player:GetRankInGroup(Group) == 6 then
				flag.Parent = player.Backpack
			end
		end
	end)
end)

This one gives people in my group different weapons and same thing happens here, when they die/reset they lose the weapons they were given except the one I have put in “StarterPack”, please explain why this happens, I’m confused.

Just put the “items cloned variables” into the character added event function

--Incorrect
local hat1 = item:Clone()

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)

--Correct
game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
        local hat1 = item:Clone()

You need to clone them every time it inserts the tool into the players backpack/adds the accessory to their character.
Otherwise it clones it only once and tries to change the same clones parent for every player that joins.

local flintlock = game.ServerStorage.Weapons.Flintlock
local flag = game.ServerStorage.Weapons.Colour
local Group = 12770278


game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		if player.TeamColor == BrickColor.new("Maroon") then
			print("Onteam")
			if player:GetRankInGroup(Group) >= 5 then 
                local ThisPlayersFlintLock = flintlock:Clone()
				flintlock.Parent = player.Backpack
			elseif player:GetRankInGroup(Group) == 6 then
                local ThisPlayersFlag = flag:Clone()
				ThisPlayersFlag.Parent = player.Backpack
			end
		end
	end)
end)

Pretty much same thing with the first script.
Make sure to put the clone inside the elseif so it doesn’t create a bunch of clones and end up not using them because of the RankInGroup not matching.

Thank you that helped now I just need to fix another thing, I have this function inside that removes a players accessories, it works but only sometimes,

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Wait()
	player.Character.Humanoid:RemoveAccessories()
end)

This function

This helped as well, thank you!

Works fine for me.

Might be the way you handle the PlayerAdded.

Try this with the accesory removing lines put inside of the same function.

local players = game:GetService("Players")
local hat = game.ServerStorage.Accessories.RankerHat:Clone()
local hat2= game.ServerStorage.Accessories.NerdHair:Clone()
local hat3 = game.ServerStorage.Accessories.OfficerHat:Clone()
local Group = 12770278

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		player.CharacterAppearanceLoaded:Wait()
		player.Character.Humanoid:RemoveAccessories()
		local hum = character:WaitForChild("Humanoid")
		if player:GetRankInGroup(Group) == 254 then
			wait(3)
			hum:AddAccessory(hat3)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) == 253 then
			wait(3)
			hum:AddAccessory(hat3)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) == 10 then	
			wait(3)
			hum:AddAccessory(hat3)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) == 9 then
			wait(3)
			hum:AddAccessory(hat3)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) == 8 then
			wait(3)
			hum:AddAccessory(hat3)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) == 7 then
			wait(3)
			hum:AddAccessory(hat3)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) == 6 then	
			wait(3)
			hum:AddAccessory(hat3)
			hum:AddAccessory(hat2)
		elseif player:GetRankInGroup(Group) <= 5 then
			wait(3)
			hum:AddAccessory(hat)
			hum:AddAccessory(hat2)
		end
	end)
end)
1 Like

Thank you again! This helped! I appreciate the help

1 Like

Btw I recommend you change that if statement to a single one to avoid nesting, avoid constant GetRankInGroup request, looks nicer, shorter code = better.

something like this

local rank = player:GetRankInGroup(Group)
if rank >=253 or (rank <= 10 and rank > 5) then
	hum:AddAccessory(hat3)
	hum:AddAccessory(hat2)
elseif rank <= 5 then
	hum:AddAccessory(hat)
	hum:AddAccessory(hat2)
end
1 Like

Alright I will do that later, thank you!