My head is exploding and I'm not sure how to fix this

I have this script in a part, but it doesn’t seem to be working. There’s no errors and I’m very stuck.

task.wait(4)

local characters = workspace.CharactersInGame:GetChildren()
local character = nil
local root = nil

local module = {}

local Rep = game:GetService("ReplicatedStorage")

local moving = false

function module.MoveTo(Part, Part2)
	local waypoints = workspace:WaitForChild("Map"):WaitForChild("Waypoints")

	local function Move(waypoint, sphere)
		moving = true
		local currentPosition = sphere.Position
		local designatedPosition = waypoint.Position

		local cPositionX = sphere.Position.X -- current
		local cPositionY = sphere.Position.Y
		local cPositionZ = sphere.Position.Z

		repeat
			task.wait()
			sphere.CFrame = CFrame.new(sphere.Position, waypoint.Position)

			local dPositionX = designatedPosition.X  -- designated
			local dPositionY = designatedPosition.Y
			local dPositionZ = designatedPosition.Z

			if cPositionX < dPositionX then
				sphere.Position += Vector3.new(1, 0, 0)
			elseif cPositionX > dPositionX then
				sphere.Position -= Vector3.new(1, 0, 0)
			end

			if cPositionY < dPositionY then
				sphere.Position += Vector3.new(0, 1, 0)
			elseif cPositionY > dPositionY then
				sphere.Position -= Vector3.new(0, 1, 0)
			end

			if cPositionZ < dPositionZ then
				sphere.Position += Vector3.new(0, 0, 1)
			elseif cPositionZ > dPositionZ then
				sphere.Position -= Vector3.new(0, 0, 1)
			end

			currentPosition = sphere.Position
			designatedPosition = waypoint.Position

			cPositionX = currentPosition.X
			cPositionY = currentPosition.Y
			cPositionZ = currentPosition.Z

			dPositionX = designatedPosition.X
			dPositionY = designatedPosition.Y
			dPositionZ = designatedPosition.Z
		until (currentPosition - designatedPosition).Magnitude < 2

		moving = false
		print("finished")
	end

	while task.wait() do
		if (Part2.Position -  Part.Position).Magnitude < 1 then return end

		for index, waypoint in pairs(waypoints:GetChildren()) do
			if (waypoint.Position - Part2.Position).Magnitude < 2 then
				if not moving then Move(waypoint, script.Parent) end
			end
		end
	end
end


while task.wait(8) do
	characters = workspace.CharactersInLobby:GetChildren()
	character = characters[math.random(1, #characters)]
	root = character:WaitForChild("HumanoidRootPart")
	module.MoveTo(script.Parent, root)
	print("move")
end

What is this supposed to be doing in this part?

1 Like

It’s supposed to make x part go to y part, and to get to that part, it will go to waypoints that are closest to the player.

I’m not sure why this is not working though.

are you using a module script or a server script?

1 Like

I was using a module script before, but completely forgot to just make a regular function in the script after I copied and pasted the code. I can change the way it is to make it in a function if that helps in any possible way.

it still should work correctly if its in a server script. It seems that something must be yielding the entire script. Try putting print("blah blah blah") in places and see where in the script its having the problem.

1 Like

You could probably use tweens to simplify the movement. If that’s not the issue, then start spamming prints.

2 Likes

Make sure the part isnt anchored, and your cosntraints have the correct amount of force to move the part

1 Like

I did eventually get an error, thank god.

  20:55:16.115  Workspace.BotSphere.FloatToPoint:85: invalid argument #2 to 'random' (interval is empty)  -  Server - FloatToPoint:85
  20:55:16.121  Stack Begin  -  Studio
  20:55:16.122  Script 'Workspace.BotSphere.FloatToPoint', Line 85  -  Studio - FloatToPoint:85
  20:55:16.122  Stack End  -  Studio

My player.Character is in the folder though, which confuses me.

For me, this is more simpler, I don’t really like using tweens all of the time for moving parts, unless I want the game to actually be smooth.

The part’s supposed to be anchored, becuase if it wasn’t, it would have rolled away and would keep trying to roll away between the time’s of when the Move() function is fired.

so are you cframing the part to make it move?

1 Like

This is error is basically saying that workspace.CharactersInLobby:GetChildren() is equal to 0 or nil, maybe run some tests and see why? also, make sure the characters are being put inside the folder on the server side.

1 Like

I looked in the explorer, and I was inside of the folder when I joined the game.

I’m positioning it instead.

use cframe, so you don’t deal with collision

Could you elaborate on this?

I don’t know if this will help you any in moving your parts, but I created this place to help with another post. However it uses CFrames and Tweening to move the door open and closed.
AutomaticSlidingDoor.rbxl (41.4 KB)

And this other place file, for yet another post, where they needed an object to follow a set of waypoints.
FollowWaypoints.rbxl (74.4 KB)

Maybe something in there can help you.
Sorry I couldn’t do more.