Ok, first i call my function to return an object so i can clone it:
1 | local newActualPad = closeByCheck(currentPly):Clone() |
closeByCheck has to return me an object so i can clone it
01 | local closeByCheck = function (ply) |
02 | local part |
03 | print ( "closeByCheck" ) |
04 | local signal |
05 | signal = ply:FindFirstChild( "Right Leg" ).Touched:connect( function (otherPart) |
06 | print ( "Touched" ) |
07 | print (otherPart.Parent) |
08 | if otherPart.Parent = = game.Workspace.Grid then |
09 | print ( "Part is grid" ) |
10 | if (ply.HumanoidRootPart.Position - otherPart.Position).magnitude < startRange then |
11 | print ( "Part is close" ) |
12 | part = otherPart |
13 | print (part.Name) |
14 | end |
15 | end |
16 | signal:disconnect() |
17 |
18 | end ) |
19 | return part |
20 | end |
When i run it, closeByCheck returns me nothing, and when the script is gonna clone, error, attempt to index a 'nil'
A connection doesn't take time. Your code executes like this:
part = nil
return part
Since part (2.) is done immediately, your closeByCheck
might as well be return nil
.
You could wait until part
is not nil
, for example:
1 | repeat |
2 | wait() |
3 | until part |
4 | return part |
Though I'm not sure if a design that waits for a contact makes sense, since it could be a very long time before anything touches this.