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

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

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

✓ Accepted answer

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)

5 comments

0

Thank you, sir. You explained a lot to me. I will take this knowledge and learn from it :)

0

Last question, why can't I put "Part"? Is that not a filter or something?

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.

1

Both "isA" and "IsA" are correct usage of the method. As for everything else, good explanation.

1

Alright, didn't know that. I'll edit that out, thanks.

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