wait(1) player = game.Players.LocalPlayer mouse = player:GetMouse() mouse.Button1Down:connect(function(click) if mouse.Target~=nil then --Check to make sure it wont break if mouse.Target.Name == "ATM" then --Needs to check its name if mouse.Target:isA("Part") then mouse.Target:clone() mouse.Target.Parent = player.backpack elseif mouse.Target == nil then print("Error") end end end end)
Hi, thank you for reading. What I'm trying to do is that when you click on a part, the part will clone into the players backpack. I use mouse.Target, but it doesn't seem to work. Can anyone help me?
You should use "BasePart" as a filter instead of "Part". In line 9 you spelled "backpack" wrong, it should be "Backpack". You also don't need to check to see whether the target is nil or not, you simply need to check to see if it exists. in line 8 you make a clone of the mouse but do not assign it to a variable, so it cannot be referenced and is eaten up by the merciless garbage collector. In line 9, you change the mouse's target to the backpack instead of a clone of the target. Additionally, you used several if then
sequences when you could've simply used and
.
Finished Script:
wait(1) player = game.Players.LocalPlayer mouse = player:GetMouse() mouse.Button1Down:connect(function(click) if mouse.Target and mouse.Target.Name == "ATM" and mouse.Target:IsA("BasePart") then local clone = mouse.Target:clone() clone.Parent = player.Backpack else print("Error") end end)