I want to clone anything (including humanoids, items, etc..) that touches my part. Can someone help me with this?
The three answers above this are correct but you won't see anything until you clone soo many things it will lag down the game.
The correct way to Clone a touching part would be:
01 | local Part = script.Parent -- Clone the item if if touched this part |
02 | local deBounce = false -- a check to stop any parts from spamming the touched event over and over |
03 | Part.Touched:Connect( function (hit) |
04 | if (deBounce = = false ) then -- check to see if anything has touched us before, if not then continue |
05 | deBounce = true -- We've been touched! |
06 | local Cne = hit:Clone() -- Clonbe the thing that touched us |
07 | Cne.Parent = workspace -- put the newlly created part inside the workspace |
08 | wait( 1 ) -- wait a second |
09 | deBounce = false -- phew nothings touching us! |
10 | end |
11 | end ) |
If you wanted to stop the clones from creating clones of the one that touched the main part you would need to keep a record of all the cloned parts in a table and cycle through them and check to see if the touching part is one of the cloned parts.
something like:
01 | local Clonies = { } |
02 | local Part = script.Parent -- Clone the item if if touched this part |
03 | local deBounce = false -- a check to stop any parts from spamming the touched event over and over |
04 | Part.Touched:Connect( function (hit) |
05 | local isClone = false |
06 | for k,v in pairs (Clonies) do |
07 | if (hit = = v) then |
08 | isClone = true |
09 | break ; |
10 | end |
11 | end |
12 | if (deBounce = = false and isClone = = false ) then -- check to see if anything has touched us before, if not then continue |
13 | deBounce = true -- We've been touched! |
14 | local Cne = hit:Clone() -- Clonbe the thing that touched us |
15 | Cne.Parent = workspace -- put the newlly created part inside the workspace |
16 | Clonies [ #Clonies+ 1 ] = Cne -- add this clone to our clones table |
17 | wait( 1 ) -- wait a second |
18 | deBounce = false -- phew nothings touching us! |
19 | end |
20 | end ) |
hope this helps!
Use the .Touched event to detect when something is touched. Then use :Clone().
1 | local Part = --(Part Location)-- |
2 | local TouchingPart = --(The part that touched the original part) |
3 |
4 | Part.Touched:connect( function () |
5 | TouchingPart:Clone() |
6 | end ) |
1 | local Part = script.Parent -- Clone the item if if touched this part |
2 |
3 | Part.Touched:Connect( function (hit) |
4 | hit:Clone() |
5 | end ) |
This way, anything that touches your part will be cloned.
Ok this is a bit interesting, because EVERYTHING that touches the part gets cloned (even the parts of you character).
(Server)Script in the part:
1 | local ClonePart = script.Parent -- The part that clones everything |
2 |
3 | ClonePart.Touched:Connect( function (obj) -- Function fires, when an object touches the part |
4 | obj:Clone() -- Clone The part |
5 | end ) -- End of function |
Hopefully I can help... ;)