Help fixing clothes script

Hey, ROBLOX developers!

I want to make a system that gives a player pants and a shirt based on a value in ReplicatedStorage. (The clothing models are named after numbers)

Script:

local val = game.ReplicatedStorage.Values.PlayerNumber
local pantsId = script.Parent.Unis:WaitForChild(val.Value):WaitForChild("Pants"):WaitForChild("PantsTemplate")
local shirtId = script.Parent.Unis:WaitForChild(val.Value):WaitForChild("Shirt"):WaitForChild("ShirtTemplate")
local cPart = script.Parent.Parent.th

local function onClicked(player)
	
	local foundShirt = player.Character:FindFirstChild("Shirt")
	if not foundShirt then
		local newShirt = Instance.new("Shirt",player.Character)
		newShirt.Name = "Shirt"
	else if foundShirt then
		player.Character.Shirt:remove()
		local newShirt = Instance.new("Shirt",player.Character)
		newShirt.Name = "Shirt"
	end
	end
	
	local foundPants = player.Character:FindFirstChild("Pants")
	if not foundPants then
		local newPants = Instance.new("Pants",player.Character)
		newPants.Name = "Pants"
	else if foundPants then
		player.Character.Pants:remove()
		local newPants = Instance.new("Pants",player.Character)
		newPants.Name = "Pants"
	end
	end
	
	player.Character.Shirt.ShirtTemplate = shirtId
	player.Character.Pants.PantsTemplate = pantsId
end

cPart.Touched:Connect(onClicked)

(No errors in Output.)

Any help is appreciated!

Might be wrong but I’m pretty sure the touch event won’t give you the player straight up just whatever part is in contact with it.

You’ll have to get the player from the part via

game.Players:GetPlayerFromCharacter(touched.Parent)

Changing the player in your onClicked() function to be touched instead and then you’ll be able to access the Character like normal

I tried this script:

local val = game.ReplicatedStorage.Values.PlayerNumber
local pantsId = script.Parent.Unis:WaitForChild(val.Value):WaitForChild("Pants"):WaitForChild("PantsTemplate")
local shirtId = script.Parent.Unis:WaitForChild(val.Value):WaitForChild("Shirt"):WaitForChild("ShirtTemplate")
local cPart = script.Parent.Parent.th

local function onClicked(hit)

> 	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

local foundShirt = player.Character:FindFirstChild("Shirt")
	if not foundShirt then
		local newShirt = Instance.new("Shirt",player.Character)
		newShirt.Name = "Shirt"
	else if foundShirt then
		player.Character.Shirt:remove()
		local newShirt = Instance.new("Shirt",player.Character)
		newShirt.Name = "Shirt"
	end
	end
	
	local foundPants = player.Character:FindFirstChild("Pants")
	if not foundPants then
		local newPants = Instance.new("Pants",player.Character)
		newPants.Name = "Pants"
	else if foundPants then
		player.Character.Pants:remove()
		local newPants = Instance.new("Pants",player.Character)
		newPants.Name = "Pants"
	end
	end
	
	player.Character.Shirt.ShirtTemplate = shirtId
	player.Character.Pants.PantsTemplate = pantsId
end

cPart.Touched:Connect(onClicked)

As you can see I tried the game.Players:GetPlayerFromCharacter() function, although it does not work (no errors)