Error with script

Hi I am trying to get a string value’s value but its giving an error

script

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		if plr.Clothing:FindFirstChild("Hair").Value == "1sthair" then
			local hair1 = game.ServerStorage.Hair:WaitForChild("1sthair"):Clone()
			hair1.Name = "hair"
		elseif plr.Clothing:FindFirstChild("Hair").Value == "2ndhair" then
			local hair1 = game.ServerStorage.Hair:WaitForChild("2ndhair"):Clone()
			hair1.Name = "hair"
		end
	end)
end)

error

ServerScriptService.Script:5: attempt to index nil with 'Value'

I assume you create the Hair object somewhere else in your code.

If that’s the case, try waiting for it to load before using it:

game.Players.PlayerAdded:Connect(function(plr)
        local hair = plr:WaitForChild("Clothing"):WaitForChild("Hair")

	plr.CharacterAdded:Connect(function(char)
		if hair.Value == "1sthair" then
			local hair1 = game.ServerStorage.Hair:WaitForChild("1sthair"):Clone()
			hair1.Name = "hair"
		elseif hair.Value == "2ndhair" then
			local hair1 = game.ServerStorage.Hair:WaitForChild("2ndhair"):Clone()
			hair1.Name = "hair"
		end
	end)
end)
1 Like

Ok thank you very much I will try using this

Ohhhh Ik, yes they are not wearing any hair I made a code that removes it

Local scripts can’t access ServerStorage move those hats to ReplicatedStorage instead.

Its not a local script its a server script in ServerScriptService

if plr.Clothing:FindFirstChild("Hair").Value == "1sthair" then

Is the issue, you’re attempting to look for a child named “Clothing” inside player then you’re attempting to look for a child named “Hair” inside “Clothing” then you’re attempting to access the “Value” property of “Hair” which in this instance is nil.

You likely have the wrong reference defined or those instances aren’t correctly made & placed in the correct positions.

Umm no actually I made a folder inside the player and when the player changes their character in-game it saves to the string value that’s how I made my datastore work

image

I think Ik why its not working, well it is but I have not parented the clone so it does not go any where I will fix that and thank you everyone who helped

Then why does the error indicate that plr.Clothing is nil?

local players = game:GetService("Players")
local storage = game:GetService("ServerStorage")

players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		if plr:WaitForChild("Clothing"):FindFirstChild("Hair").Value == "1sthair" then
			local hair1 = storage:WaitForChild("Hair"):WaitForChild("1sthair"):Clone()
			hair1.Name = "hair"
		elseif plr:WaitForChild("Clothing"):FindFirstChild("Hair").Value == "2ndhair" then
			local hair1 = storage:WaitForChild("Hair"):WaitForChild("2ndhair"):Clone()
			hair1.Name = "hair"
		end
	end)
end)

I’ve added waits to everything, add the fixes you needed to implement.

1 Like