1 | |
2 | |
3 | local Player = game.Players.LocalPlayer |
4 | local Character = Player.Character or Player.CharacterAdded:Wait() |
5 | |
6 | local function onCharacterAdded(char) |
7 | Character = char |
8 | HumanoidRootPart = Character:WaitForChild("HumanoidRootPart") |
9 | end |
10 | |
11 | Player.CharacterAdded:Connect(onCharacterAdded) |
12 | |
13 | repeat wait() until Character and Character:FindFirstChild("HumanoidRootPart") |
14 | |
15 | local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart") |
16 | |
17 | local coinName = "Coin" |
18 | |
19 | |
20 | local function findCoinsInWorkspace(parent, coins) |
21 | for _, child in ipairs(parent:GetChildren()) do |
22 | if child:IsA("Model") or child:IsA("Folder") then |
23 | findCoinsInWorkspace(child, coins) |
24 | elseif child:IsA("BasePart") and child.Name == coinName then |
25 | table.insert(coins, child) |
26 | end |
27 | end |
28 | end |
29 | |
30 | local function teleportToPosition(position) |
31 | HumanoidRootPart.CFrame = CFrame.new(position) |
32 | end |
33 | |
34 | local function teleportToCoins() |
35 | local coins = {} |
36 | findCoinsInWorkspace(game.Workspace, coins) |
37 | |
38 | if #coins == 0 then |
39 | print("No coins found!") |
40 | return |
41 | end |
42 | |
43 | for _, coin in ipairs(coins) do |
44 | if coin:IsDescendantOf(workspace) then |
45 | |
46 | local distance = (HumanoidRootPart.Position - coin.Position).magnitude |
47 | |
48 | if distance < 10000000 then |
49 | |
50 | teleportToPosition(coin.Position + Vector3.new(0, 0, 0)) |
51 | |
52 | wait(0.05) |
53 | else |
54 | print("Coin is too far:", coin.Name) |
55 | end |
56 | else |
57 | print("Coin is not in the workspace anymore:", coin.Name) |
58 | end |
59 | end |
60 | end |
61 | |
62 | |
63 | while true do |
64 | teleportToCoins() |
65 | wait() |
66 | end |