Problems with addressing a local player

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)

1 Like

ServerStorage

you cannot get the server storage from the client, youll have to make a remove event

Okay, how would I go about doing that?

actually you could do this all on the server lol

connect Players.PlayerAdded to a function that checks their leaderstat and then gives them the corresponding hat

1 Like

Alrighty let me try that out, let ya know if it works

heres something i made real quick

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

Noticed I did the function wrong I’m using yours rq

also make sure you make it a serverscript

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

you needed the CharacterAdded event, just use the one i made and add the part where it gives you the hat depending on your leaderstat valie

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)

Just tried and it doesn’t work or give an error for some reason

replace add this to the leaderstats part

local leaderstats = player:WaitForChild("leaderstats", 5)
if not leaderstats then return end
local equippedhat = leaderstats:WaitForChild("Equiped Hat", 5)

Still not working. This is so weird

does all of the stuff exist then

because if its not giving you an error it means the leaderstats doesnt exist

It does exsist, I’ll send a screenshot


Here, even tried switching from the crown to the pan hat

This is all really strange because I have some proximity prompts that equip the hat and change the Equipped hat leaderstat and that works fine

do you want a tutor…? ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎