Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make it so when you click the part it will give you a shirt and pants?

So I have this part with a ClickDetector and Inside that ClickDetector, I have a shirt and pants.

So when you Click the part it will give you the shirt and pants but it does not seem to be working.

Here is the script:

function Click(mouse)
 
	local pName = script.Parent.Parent.Parent.Parent.Parent.Name
	local pPlayer = game.Workspace:findFirstChild(pName)
		
		for j,k in pairs(pPlayer:GetChildren()) do
			if k:IsA("Pants") or k:IsA("Shirt") then
				k:destroy()
			end
		end
		
		script.Parent.Pants:clone().Parent = pPlayer
		script.Parent.Shirt:clone().Parent = pPlayer
		
end

script.Parent.MouseClick:connect(Click)

Output: 21:56:42.011 - Workspace.E2 Uniform Case.ClickPart.Head.ClickDetector.Clothes:6: attempt to index nil with 'GetChildren'

4 comments

0

On line 3, you have "script.Parent.Parent.Parent.Parent.Parent". Is this supposed to be the player? Can you describe where the script is?

0

The script is inside the Click Detector

0

Ok. Can you tell me what pName is?

0

Actually, I know a fix. I'll write out an answer.

2 answers

✓ Accepted answer

When you connect a function with the MouseClick event, the first argument passed will always be the player. So, for example, if I were to write:

local detector = script.Parent

function onClick(player) -- This first argument is the player
	print(player, " clicked me!")
end

detector.MouseClick:connect(onClick)

The output would be: MrBlockyhead clicked me!

You don't have to call it player; you can call it anything you want. Just know that this first argument is the player. Then, to get the character in the workspace from this, all you have to reference is player.Character. In the code you provided, the first argument you have is "mouse". This is actually indexing the player! So, to fix your code, try this:

function Click(player)

    local character = player.Character

        for j,k in pairs(character:GetChildren()) do
            if k:IsA("Pants") or k:IsA("Shirt") then
                k:destroy()
            end
        end

        script.Parent.Pants:clone().Parent = character
        script.Parent.Shirt:clone().Parent = character

end

script.Parent.MouseClick:connect(Click)

2 comments

0

Thank you so much! and you have helped me learned something new! :D

0

No problem, glad I could help! :)

Here is the script for the shirt and pants I hope it helps please consider accepting this is it helps!


local CLICK_BLOCK = script.Parent
local ITEM_ID = 0 --ID number of shirt/pants

local Click = Instance.new("ClickDetector",CLICK_BLOCK)

Click.MouseClick:connect(function(p)
game:GetService("MarketplaceService"):PromptPurchase(p,ITEM_ID)
end)

2 comments

0

I dont want people to buy the shirt and pants I want it so when you click the click detector it will give you the shirt and pants in game

0

Ohhhh

This discussion is preserved as it was. New answers, comments, and votes are closed.