Help with "WaitForChild"

Hello! i made this script, and i dont see any error on it, but on console says image
this is my code, can someone help?

local Pantsggj = game.Players.LocalPlayer:WaitForChild("PlayerOldPantsId")
local Shirtsggj = game.Players.LocalPlayer:WaitForChild("PlayerOldShirtId")
local ChangerPart = script.Parent
local ShirtTemplate = "http://www.roblox.com/asset/?id=aniceidhere"
local PantsTemplate = "http://www.roblox.com/asset/?id=aniceidhere"

ChangerPart.Touched:Connect(function(TouchPart)
	if TouchPart and TouchPart.Parent and TouchPart.Parent:FindFirstChild("Humanoid") then
		print(TouchPart.Parent.Name ..  " - Changed clothes")
		local Character = TouchPart.Parent
		local shirt = Character:FindFirstChildOfClass("Shirt")
		local pants = Character:FindFirstChildOfClass("Pants")
		if not shirt then 
			shirt = Instance.new("Shirt", Character)
			shirt.Name = "Shirt"
		end 
		if not pants then 
			pants = Instance.new("Pants", Character)
			pants.Name = "Pants"
		end
		Character.Shirt.ShirtTemplate = ShirtTemplate
		Character.Pants.PantsTemplate = PantsTemplate
		wait(3)
		Character.Shirt.ShirtTemplate = Shirtggj.Value
		Character.Pants.PantsTemplate = Pantsggj.Value
	end
end)

This is most likely inside of a Script and not a LocalScript. .LocalPlayer can only be used in LocalScripts and not Scripts

1 Like

Now the script dont works and dont gives any error on console too…

Best if you got the player from the touched connection by using:

game.Players:GetPlayerFromCharacter(characterInstance)
1 Like

You can get the player in a different way, keep it in a serverscript, but change it to this

local ChangerPart = script.Parent
local ShirtTemplate = "http://www.roblox.com/asset/?id=aniceidhere"
local PantsTemplate = "http://www.roblox.com/asset/?id=aniceidhere"

ChangerPart.Touched:Connect(function(TouchPart)
	local player = game.Players:GetPlayerFromCharacter(TouchPart)
	if TouchPart and TouchPart.Parent and TouchPart.Parent:FindFirstChild("Humanoid") and player then
		print(TouchPart.Parent.Name ..  " - Changed clothes")
		
		local Pantsggj = player:WaitForChild("PlayerOldPantsId")
		local Shirtsggj = player:WaitForChild("PlayerOldShirtId")
		
		local Character = TouchPart.Parent
		
		local shirt = Character:FindFirstChildOfClass("Shirt")
		local pants = Character:FindFirstChildOfClass("Pants")
		
		if not shirt then 
			shirt = Instance.new("Shirt", Character)
			shirt.Name = "Shirt"
		end 
		
		if not pants then 
			pants = Instance.new("Pants", Character)
			pants.Name = "Pants"
		end
		
		Character.Shirt.ShirtTemplate = ShirtTemplate
		Character.Pants.PantsTemplate = PantsTemplate
		wait(3)
		Character.Shirt.ShirtTemplate = Shirtggj.Value
		Character.Pants.PantsTemplate = Pantsggj.Value
	end
end)

Like @MixedConscience said, you should use GetPlayerFromCharacter, which with this, should work since if it doesn’t find a player with the parent of hit, then it will give nil

2 Likes

Sorry, it dont works and dont give any error on console
(Sorry for take 11 minutes for reply, i was eating.)

Does it print the print thing in the if statement? If it doesn’t, then something is returning false

1 Like

Where is the script? Is it in the part the player should touch?

1 Like

Yeah, its on the part the player touch

No, it dont print anything on console

Okay it’s my fault

local player = game.Players:GetPlayerFromCharacter(TouchPart)

Change it to

local player = game.Players:GetPlayerFromCharacter(TouchPart.Parent)
1 Like

aaaaa It worked! thank you :heart:

1 Like

Anyimt! If you have anymore issues don’t be afraid to make another post!

1 Like