Using a while loop in script without stopping the rest of the script from running

I’m using a while loop to ray cast but I also need the rest of the script to run. The while loop is stopping this and I’m not sure how to stop it form stopping the rest of the script.
The script:

local raycast2 = Ray.new(part.Position, Vector3.new(-30, 0, 0))

while wait(1) do
	local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(raycast, {part})
	if hit then
		distance1 = part.Position.X - hit.Position.X
		print(distance1)
		print("racast intercected")
	end
end
1 Like

Run the loop at the very bottom of the script so everything else is above it

1 Like

Thats the thing, i need to process the data that the raycast gives me, and i cant do that if it hasnt been created yet.
Heres the rest of the script:

local part = script.Parent

distance1 = 0
distance2 = 0

local raycast = Ray.new(part.Position, Vector3.new(30, 0, 0))

while wait(1) do
	local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(raycast, {part})
	if hit then
		distance2 = part.Position.X - hit.Position.X
		print(distance2)
		print("racast intercected")
	end
end

local raycast2 = Ray.new(part.Position, Vector3.new(-30, 0, 0))

while wait(1) do
	local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(raycast, {part})
	if hit then
		distance1 = part.Position.X - hit.Position.X
		print(distance1)
		print("racast intercected")
	end
end

if distance1 > distance2 then
	script.Parent.VectorForce.Force.Z = 200
	script.Parent.VectorForce.Force.Y = 0
	print("left")
elseif 
	distance1 < distance2 then
	script.Parent.VectorForce.Force.Z = 0
	script.Parent.VectorForce.Force.Y = 200
	print("right")
elseif distance1 == distance2 then
	script.Parent.VectorForce.Force.Z = 0
	script.Parent.VectorForce.Force.Y = 0
	print("equalibrium!")
end
1 Like

You could put the if statements into a function and fire the function in the while loops

2 Likes

Sounds like it would work but it didn’t for me. I’m probably doing something wrong but i cant see it. Also i just put it in a while loop instead of a function since I dont need it to be a function

local part = script.Parent

distance1 = 0
distance2 = 0
	
while wait(1) do	
	local raycast = Ray.new(part.Position, Vector3.new(30, 0, 0))

	local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(raycast, {part})
	if hit then
		distance2 = part.Position.X - hit.Position.X
		print(distance2)
		print("racast intercected")
	end

	local raycast2 = Ray.new(part.Position, Vector3.new(-30, 0, 0))


	local hit, position = game.Workspace:FindPartOnRayWithIgnoreList(raycast, {part})
	if hit then
		distance1 = part.Position.X - hit.Position.X
		print(distance1)
		print("racast intercected")
	end

	if distance1 > distance2 then
		script.Parent.VectorForce.Force.Z = 200
		script.Parent.VectorForce.Force.Y = 0
		print("left")
	elseif 
		distance1 < distance2 then
		script.Parent.VectorForce.Force.Z = 0
		script.Parent.VectorForce.Force.Y = 200
		print("right")
	elseif distance1 == distance2 then
		script.Parent.VectorForce.Force.Z = 0
		script.Parent.VectorForce.Force.Y = 0
		print("equalibrium!")
	end
end
1 Like

Is there anything in the output?

1 Like

Yeah, its an error. Says

 14:37:31.540  Z cannot be assigned to  -  Server - Script:36

that line of the script changes the vector force Z to 0. I have no idea why that could be an error

		script.Parent.VectorForce.Force.Z = 200
		script.Parent.VectorForce.Force.Y = 0
		print("left")
	elseif 
		distance1 < distance2 then
		script.Parent.VectorForce.Force.Z = 0
		script.Parent.VectorForce.Force.Y = 200
		print("right")
	elseif distance1 == distance2 then
36		script.Parent.VectorForce.Force.Z = 0
		script.Parent.VectorForce.Force.Y = 0
		print("equalibrium!")
	end
end

edit: i resolved this by just removing that part since it wasnt needed

2 Likes

You can’t directly assign doubles (double precision numbers) to the ‘X’, ‘Y’ or ‘Z’ components of a Vector3 datatype value, you need to assign a Vector3 datatype value directly.

VectorForce.Force = Vector3.new(0, 0, 200)
VectorForce.Force = Vector3.new(0, 200, 0)
VectorForce.Force = Vector3.new(200, 0, 0)
2 Likes