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
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
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
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.