Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Cloning a part to your backpack when you click on it?

Asked by
Yeevivor4 155
9 years ago
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?

1 answer

Log in to vote
2
Answered by 9 years ago

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)
0
Thank you, sir. You explained a lot to me. I will take this knowledge and learn from it :) Yeevivor4 155 — 9y
0
Last question, why can't I put "Part"? Is that not a filter or something? Yeevivor4 155 — 9y
0
No problem. Part works as a filter, it's just that BasePart is a classname, so it works for a wide variety of parts instead of simply normal parts. aquathorn321 858 — 9y
1
Both "isA" and "IsA" are correct usage of the method. As for everything else, good explanation. Tkdriverx 514 — 9y
1
Alright, didn't know that. I'll edit that out, thanks. aquathorn321 858 — 9y
Ad

Answer this question