Hey, so right now I’m trying to make a system where the player joins, it equips a hat. Problem is, I’m getting a simple error that I don’t understand why I’m getting. This script is in ServerScriptService,
local player = game.Players.LocalPlayer
local hatFolder = game:GetService("ServerStorage").Hats
local hatClone = hatFolder.EgglessFryingPanAccessory:Clone()
local hatClone2 = hatFolder.KnightHelmet:Clone()
local hatClone3 = hatFolder["Black Top Hat with Red Band"]:Clone()
local hatClone4 = hatFolder.BlackFedora:Clone()
local hatClone5 = hatFolder["The True King"]:Clone()
if player.leaderstats["Equiped Hat"].Value == "Frying Pan" then
hatClone.parent = player.Character
end
if player.leaderstats["Equiped Hat"].Value == "Knight Helm" then
hatClone2.parent = player.Character
end
if player.leaderstats["Equiped Hat"].Value == "Top Hat" then
hatClone3.parent = player.Character
end
if player.leaderstats["Equiped Hat"].Value == "Fedora" then
hatClone4.parent = player.Character
end
if player.leaderstats["Equiped Hat"].Value == "Crown" then
hatClone5.parent = player.Character
end
The error I’m getting: ServerScriptService.HatEquipScript:11: attempt to index nil with ‘WaitForChild’ (LINE 11)
local hats = game.ServerStorage:WaitForChild("Hats")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then return end
local hat = leaderstats:FindFirstChild("Equiped Hat")
if hat and hat.Value == "Fedora" then
-- give them the fedora
end -- and so on for the other hats
end)
end)
local hatFolder = game:GetService("ServerStorage").Hats
if game.Players.PlayerAdded then
local hatClone = hatFolder.EgglessFryingPanAccessory:Clone()
local hatClone2 = hatFolder.KnightHelmet:Clone()
local hatClone3 = hatFolder["Black Top Hat with Red Band"]:Clone()
local hatClone4 = hatFolder.BlackFedora:Clone()
local hatClone5 = hatFolder["The True King"]:Clone()
if player.leaderstats["Equiped Hat"].Value == "Frying Pan" then
hatClone.parent = player.Character
end
if player.leaderstats["Equiped Hat"].Value == "Knight Helm" then
hatClone2.parent = player.Character
end
if player.leaderstats["Equiped Hat"].Value == "Top Hat" then
hatClone3.parent = player.Character
end
if player.leaderstats["Equiped Hat"].Value == "Fedora" then
hatClone4.parent = player.Character
end
if player.leaderstats["Equiped Hat"].Value == "Crown" then
hatClone5.parent = player.Character
end
end
Heres the new script. Still get the same error about the leaderstats
local hatFolder = game:GetService("ServerStorage").Hats
game.Players.PlayerAdded:Connect(function(player)
local hatClone = hatFolder.EgglessFryingPanAccessory:Clone()
local hatClone2 = hatFolder.KnightHelmet:Clone()
local hatClone3 = hatFolder["Black Top Hat with Red Band"]:Clone()
local hatClone4 = hatFolder.BlackFedora:Clone()
local hatClone5 = hatFolder["The True King"]:Clone()
if player.leaderstats["Equiped Hat"].Value == "Frying Pan" then
hatClone.parent = player.Character
end
if player.leaderstats["Equiped Hat"].Value == "Knight Helm" then
hatClone2.parent = player.Character
end
if player.leaderstats["Equiped Hat"].Value == "Top Hat" then
hatClone3.parent = player.Character
end
if player.leaderstats["Equiped Hat"].Value == "Fedora" then
hatClone4.parent = player.Character
end
if player.leaderstats["Equiped Hat"].Value == "Crown" then
hatClone5.parent = player.Character
end
end)
New script but gives no error and doesn’t clone the hat
i was being lazy but here i added the part where it gives them the hat for you
local hatFolder = game:GetService("ServerStorage").Hats
local hatClone = hatFolder.EgglessFryingPanAccessory:Clone()
local hatClone2 = hatFolder.KnightHelmet:Clone()
local hatClone3 = hatFolder["Black Top Hat with Red Band"]:Clone()
local hatClone4 = hatFolder.BlackFedora:Clone()
local hatClone5 = hatFolder["The True King"]:Clone()
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then return end
local equippedhat = leaderstats:FindFirstChild("Equiped Hat")
if equippedhat and equippedhat.Value == "Frying Pan" then
hatClone.parent = character
end
if equippedhat and equippedhat.Value == "Knight Helm" then
hatClone2.parent = character
end
if equippedhat and equippedhat.Value == "Top Hat" then
hatClone3.parent = character
end
if equippedhat and equippedhat.Value == "Fedora" then
hatClone4.parent = character
end
if equippedhat and equippedhat.Value == "Crown" then
hatClone5.parent = character
end
end)
end)
local leaderstats = player:WaitForChild("leaderstats", 5)
if not leaderstats then return end
local equippedhat = leaderstats:WaitForChild("Equiped Hat", 5)