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)
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)
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)
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.