Having trouble getting PlayerAdded, and CharacterAdded in ClickDetecoter Event

I’m getting an error when I put this code in, Its saying attemp to index nil with player, which is the first arguement I passed in the clickdetector event.
image

LeftClickToBuy.MouseButton1Click:Connect(function(player)
			--Need to make some CharacterAdded function
			
			player.PlayerAdded:Connect(function(Plyer)
				Plyer.CharacterAdded:Wait()
				local Char = Plyer.Character
					Char.Shirt.ShirtTemplate =ClotheModel1ShirtTemplate
					Char.Pants.PantsTemplate = ClotheModel1PantsTemplate
					--Dissconnect function 
				end)
			end)
			
		end)
	```
1 Like

You must have copy-pasted some code from a server script here, and you’re probably new to programming generally. Here’s a solution that makes sense:

LeftClickToBuy.MouseButton1Click:Connect(function()
	local Char = game:GetService("Players").LocalPlayer.Character
	Char.Shirt.ShirtTemplate =ClotheModel1ShirtTemplate
	Char.Pants.PantsTemplate = ClotheModel1PantsTemplate
end)

Yeah I’m a bit new to scripting, I’m gonna try that script out now

Wow it worked! What did you do??

Code should speak for itself as much as possible, and in this case you can follow it well:

On this line, we establish that when you click LeftClickToBuy, it will run the indented code below it:

This is the indented code:

This gets your current character:

These change your clothes:

Note that if you are wearing pants, but you are not wearing a shirt, then your pants will not change. This is because the code will try to change Char.Shirt.ShirtTemplate, but Char.Shirt isn’t there, and so the indented code errors and stops before it gets to change your pants.

This is called an edge case, which is a term for something happening in your code that isn’t common and you might forget to think about, but will cause bugs if you aren’t careful.

Here’s a solution that solves for the “wearing no shirt” edge case:

LeftClickToBuy.MouseButton1Click:Connect(function()
	local Char = game:GetService("Players").LocalPlayer.Character
	for clothingType, clothingTemplate in {Shirt = ClotheModel1ShirtTemplate, Pants = ClotheModel1PantsTemplate} do
		pcall(function()
			Char:FindFirstChildWhichIsA(clothingType)[clothingType .. "Template"] = clothingTemplate
		end)
	end
end)

Definitely not as readable, and not really even worth trying to explain to someone new to programming. It doesn’t “speak for itself” as nicely, but try to figure out what’s going on there if you like. It works and it doesn’t use copy-pasted code anywhere, which is one of the most important parts of programming.

That’s not all there is, unfortunately. You will notice eventually that only your clothes change. You do not see other peoples’ clothes changing, nor do they see it when you change your clothes. This is because the clothes changing code is in a LocalScript, which means that it’s only being run on your computer. As someone who is new to programming, I don’t know if you’re quite up for the challenge of replication — the catch-all term which basically means “making it so multiple players see the same thing happen.”

1 Like

Firing to the server rn with a remote event so everyone can see the clothes

Give it a try and report back. I doubt this will be my last post here, haha

Okay Yeah I have a few questions, So I have 3 different click detectors for each of the outfits, do I copy and paste that code and just change it up to the right outfit variable name? Also what exactly do I write on the server script??


function ShopClotheCamTween()
	
	local Char = game:GetService("Players").LocalPlayer.Character
	--TweenStuff

	
	
	--CAMERAS
	local ClotheCam1 = CameraFolder:WaitForChild("ClotheCam1")
	local ClotheCam2 = CameraFolder:WaitForChild("ClotheCam2")
	local ClotheCam3 = CameraFolder:WaitForChild("ClotheCam3")
	
	--CLOTHES
	local Clothes1 = ClothesFolder:WaitForChild("Clothe1")
	local Clothe2 = ClothesFolder:WaitForChild("Clothe2")
	local Clothe3 = ClothesFolder:WaitForChild("Clothe3")
	
	local ClotheModel1ShirtTemplate = ClothesFolder:WaitForChild("ClotheModel1").Shirt.ShirtTemplate
	local ClotheModel2ShirtTemplate = ClothesFolder:WaitForChild("ClotheModel2").Shirt.ShirtTemplate
	local ClotheModel3ShirtTemplate = ClothesFolder:WaitForChild("ClotheModel3").Shirt.ShirtTemplate
	
	local ClotheModel1PantsTemplate = ClothesFolder:WaitForChild("ClotheModel1").Pants.PantsTemplate
	local ClotheModel2PantsTemplate = ClothesFolder:WaitForChild("ClotheModel2").Pants.PantsTemplate
	local ClotheModel3PantsTemplate = ClothesFolder:WaitForChild("ClotheModel3").Pants.PantsTemplate
	
	---CLICKDETECTORS
	local ClickDetector1 = Instance.new("ClickDetector")
	ClickDetector1.Name = "ClickDetector1"
	ClickDetector1.Parent = Clothes1

	local ClickDetector2 = Instance.new("ClickDetector")
	ClickDetector2.Name = "ClickDetector2"
	ClickDetector2.Parent = Clothe2

	local ClickDetector3 = Instance.new("ClickDetector")
	ClickDetector3.Name = "ClickDetector3"
	ClickDetector3.Parent = Clothe3
	
	local info = TweenInfo.new(1,Enum.EasingStyle.Sine, Enum.EasingDirection.Out,0,false,0)
	local BuyGuiGoal = {Visible = true}

	local BuyGuiTweenCreated = tweenservice:Create(BuyGuiFrame, info, BuyGuiGoal)
	
	ClickDetector1.MouseClick:Connect(function(player)
		stepped:Disconnect()
		
		--Variables
		local LeftClickToBuy = script.Parent:WaitForChild("BuyGui"):WaitForChild("Display"):WaitForChild("Left click to buy")
		local goal = {CFrame = ClotheCam1.CFrame}
		local ClotheCam1Tween = tweenservice:Create(Camera, info, goal)
		
		-- Coding
		
		ClotheCam1Tween:Play()
		BuyGui.Enabled = true
		BuyGuiTweenCreated:Play()
		
		BuyGuiFrame:WaitForChild("Clothing Name").Text = "Firebending Robe"
		BuyGuiFrame:WaitForChild("Cost").Text = "50"
		
		LeftClickToBuy.MouseButton1Click:Connect(function()
			--Need to make some CharacterAdded function
		
			for clothingType, clothingTemplate in {Shirt = ClotheModel1ShirtTemplate, Pants = ClotheModel1PantsTemplate} do
				pcall(function()
					Char:FindFirstChildWhichIsA(clothingType)[clothingType .. "Template"] = clothingTemplate
				end)
			end
			
		end)
		
	end)

	ClickDetector2.MouseClick:Connect(function(player)
		stepped:Disconnect()
		

		local goal = {CFrame = ClotheCam2.CFrame}

		local ClotheCam2Tween = tweenservice:Create(Camera, info, goal)
		ClotheCam2Tween:Play()
		BuyGui.Enabled = true
		BuyGuiTweenCreated:Play()
		
		BuyGuiFrame:WaitForChild("Clothing Name").Text = "Firebending Kimono"
		BuyGuiFrame:WaitForChild("Cost").Text = "75"
		
		Char.Shirt.ShirtTemplate = ClotheModel2ShirtTemplate
		Char.Pants.PantsTemplate = ClotheModel2PantsTemplate
	end)

	ClickDetector3.MouseClick:Connect(function(player)
		stepped:Disconnect()
		

		local goal = {CFrame = ClotheCam3.CFrame}

		local ClotheCam3Tween = tweenservice:Create(Camera, info, goal)
		ClotheCam3Tween:Play()
		BuyGuiTweenCreated:Play()
		BuyGui.Enabled = true
		
		BuyGuiFrame:WaitForChild("Clothing Name").Text = "Firebending Red"
		BuyGuiFrame:WaitForChild("Cost").Text = "100"
		
		Char.Shirt.ShirtTemplate = ClotheModel3ShirtTemplate
		Char.Pants.PantsTemplate = ClotheModel2PantsTemplate
	end)
end

So I got it to work but when I try to get a different outfit, it doesn’t change the outfit. Does this have to do with the Table thing that you did? I don’t fully understand it.

DialogueRemote.OnServerEvent:Connect(function(player, Data)
	
	--Clothing
	local ClothesFolder = game.Workspace.Shop.Clothes
	local ClotheModel1ShirtTemplate = ClothesFolder:WaitForChild("ClotheModel1").Shirt.ShirtTemplate
	local ClotheModel2ShirtTemplate = ClothesFolder:WaitForChild("ClotheModel2").Shirt.ShirtTemplate
	local ClotheModel3ShirtTemplate = ClothesFolder:WaitForChild("ClotheModel3").Shirt.ShirtTemplate

	local ClotheModel1PantsTemplate = ClothesFolder:WaitForChild("ClotheModel1").Pants.PantsTemplate
	local ClotheModel2PantsTemplate = ClothesFolder:WaitForChild("ClotheModel2").Pants.PantsTemplate
	local ClotheModel3PantsTemplate = ClothesFolder:WaitForChild("ClotheModel3").Pants.PantsTemplate
	local Player = game:GetService("Players")
	local Char = player.Character
	
	
	if Data.Clothing == "Clothing1" then
		for clothingType, clothingTemplate in {Shirt = ClotheModel1ShirtTemplate, Pants = ClotheModel1PantsTemplate} do
			pcall(function()
				Char:FindFirstChildWhichIsA(clothingType)[clothingType .. "Template"] = clothingTemplate
			end)
		end
		
	elseif Data.Clothing == "Clothing2" then
		for clothingType2, clothingTemplate2 in {Shirt = ClotheModel2ShirtTemplate, Pants = ClotheModel2PantsTemplate} do
			pcall(function()
				Char:FindFirstChildWhichIsA(clothingType2)[clothingType2 .. "Template"] = clothingTemplate2
			end)
	end
	
	elseif Data.Clothing == "Clothing3" then 
		for clothingType3, clothingTemplate3 in {Shirt = ClotheModel3ShirtTemplate, Pants = ClotheModel3PantsTemplate} do
			pcall(function()
				Char:FindFirstChildWhichIsA(clothingType3)[clothingType3 .. "Template"] = clothingTemplate3
			end)
	end
end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.