Help with armor giver

So I recently just started scripting. On my first day, I threw this together based off of a few freemodels and tutorials I modified, so I apologize if I’m misusing certain properties or going in the completely wrong direction.

I tried making an armor giver that when clicked by a player clones the piece of armor (a block stored in the ServerStorage) and welds it to their torso. When I run it, I recieve no errors but nothing happens either. Thanks!

Here's my code:
local button = script.Parent
local clickDetector = Instance.new("ClickDetector")
	clickDetector.Parent = button
	clickDetector.MaxActivationDwistance = 10
local clonedArmor = game.Serverstorage.Chestpiece:Clone()


clickDetector.MouseClick:Connect(function(plr) --activates when clicked
local plr = clickDetector.playerWhoClicked
local char = plr.Character  
  if char.Parent:FindFirstChild("Humanoid")then --finds who clicked the button
        clonedArmor.Parent = char   
        local chest = char.Torso
        clonedArmor.CFrame = chest.CFrame --maps armor onto players chest
        local weld = Instance.new("Weld")
        weld.Part0 = chest
		weld.Part1 = clonedArmor
        weld.Parent = char
    end    
end)
2 Likes

You forgot to parent the weld, try adding:

weld.Parent = char

I had included this in a previous iteration and not sure why I took it out. However, having just tried it it doesn’t fix it.

Why this line? I am pretty sure it will work without this

1 Like

Um… Plr.parent:findfirstchild(“Humanoid”)
Shouldnt you do char:findfirstchild(“Humanoid”)
Edit: and can you clone the armor in the event?

Not sure, added it to just be safe but looking back it doesn’t seem necessary. Doesn’t fix it though.

Makes sense to clean it up. Not sure what you mean by cloning the armor in the event. I did your first suggestion but it didn’t fix anything.

Try this script

clickDetector.MouseClick:Connect(function(plr) --activates when clicked
local char = plr.Character  
  if char:FindFirstChild("Humanoid")then --finds who clicked the button
        clonedArmor.Parent = char   
        local chest = char.Torso
        clonedArmor.CFrame = chest.CFrame --maps armor onto players chest
        local weld = Instance.new("Weld")
        weld.Part0 = chest
		weld.Part1 = clonedArmor
        weld.Parent = char
    end    
end)
1 Like

you should also change weld.C1 and weld.C0

If you want this to run more then once, you can clone the armor in the event.

It’s great to see that even as a new developer you’ve made an attempt first byusing existing resources and then are asking for help on that attempted implementation, an effort that unfortunately is not that common. Thank you for that, genuinely.

So the first thing to get out of the way is the way you’re setting up this all. So far so good, other than that you parent the object first. There’s a performance warning about using the parent argument of Instance.new and parenting first also has that same effect. See here:

To this end, you would just have to flip your code around.

local clickDetector = Instance.new("ClickDetector")
clickDetector.MaxActivationDistance = 10
clickDetector.Parent = button -- Is "button" ever defined in your script?

The next part is that clonedArmor. Seeing the way that you’ve put this script together, it looks like you want to give this armour to players whenever they click, however this’ll only work once and shift the cloned instance between players. Just reference it here and when you actually want to attach it to someone, then go ahead and clone it.

local armor = game:GetService("ServerStorage").Chestpiece

Now from your ClickDetector, this is where you could reference ClickDetector documentation and perhaps also some help from PiL. PlayerWhoClicked is the argument plr in your function, not a property of the ClickDetector. You already have the player, so you can just get their character directly.

clickDetector.MouseClick:Connect(function (plr)
    local char = plr.Character
    -- Other code
end)

And then finally, from your actual code, that’s where you can work with all the variables you’ve gotten so far and applicable advice above. For example, before attaching the armour, make sure to clone it. In this case, parenting and then welding is okay to do as welds need a frame to attach to a player.

clickDetector.MouseClick:Connect(function (plr)
    local char = plr.Character
    if char:FindFirstChildOfClass("Humanoid") then
        local clonedArmor = armor:Clone()
        local torso = char.Torso -- Works only for R6
        -- Same weld code down here
    end
end)

Add that all up, and that should be a go for you.

1 Like

Off topic: Wouldn’t it be better to create the ClickDetector in studio instead of in script?

It would! I was taking the opportunity (it was my first script script) to learn about creating instances

Tried it out but not quite. Working on colbert’s suggestions right now

Alright, I think I soaked that all in, it makes sense. Here’s my updated code. It seems like it should work now but it just isn’t. I did in fact define button but it didn’t show on my formatting.

local button = script.Parent
local clickDetector = Instance.new("ClickDetector")
	clickDetector.Parent = button
	clickDetector.MaxActivationDistance = 10
local armor = game:GetService("ServerStorage").Chestpiece


clickDetector.MouseClick:Connect(function(plr) --activates when clicked
	local char = plr.Character  
  if char:FindFirstChildOfClass("Humanoid")then --finds who clicked the button 
        button.BrickColor.NewColor("Really red")
		local clonedArmor = armor:Clone()
		local chest = char.Torso
			clonedArmor.Parent = char 	
		local weld = Instance.new("Weld")
			weld.Parent = char
        	clonedArmor.CFrame = chest.CFrame --maps armor onto players chest
	        weld.Part0 = chest
			weld.Part1 = clonedArmor 
    end    
end)
2 Likes

Try putting the chestpiece in replicatedstorage

Are you aware of where you may be facing an issue? Check your console. You can either find the Output from the View menu in the ribbon bar or by accessing the F9 Developer Console. Any errors there can be reported which would better help to see where the issue may be.