Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is my if statement only running once even though it is in a while wait loop?

--variables
local body = script.Parent
local rightRay = Ray.new(body.Position,Vector3.new(0,0-10))
local leftRay = Ray.new(body.Position,Vector3.new(-10,0,0))
local backRay = Ray.new(body.Position,Vector3.new(10,0,0))
local vs = script.Parent.Parent.VehicleSeat
--start the car
vs.Throttle = 1
--do the movements every 1 second
while wait(1) do
	--this is to do trafic speed mantaince and crosswals
	local frontRay = Ray.new(body.Position,Vector3.new(0,0,-30)) 	
	local hit,Position = game.Workspace:FindPartOnRayWithIgnoreList(frontRay,{body})
	local crosswalk = game.Workspace.CrswlkObj
	if hit == crosswalk then
		print("Reached Cross walk")
		vs.Throttle = hit.Velocity.Z
		wait(1)
		vs.Throttle = 1	
	end
	
	--this is the line follower

end

Please help. Sorry if the answer is very simple. Thanks

1 answer

--variables
local body = script.Parent
local rightRay = Ray.new(body.Position,Vector3.new(0,0-10))
local leftRay = Ray.new(body.Position,Vector3.new(-10,0,0))
local backRay = Ray.new(body.Position,Vector3.new(10,0,0))
local vs = script.Parent.Parent.VehicleSeat
local RunService= game:GetService("RunService")
--start the car
vs.Throttle = 1
--do the movements every 1 second
RunService.RenderStepped:Connect(function()
    --this is to do trafic speed mantaince and crosswals
    local frontRay = Ray.new(body.Position,Vector3.new(0,0,-30))    
    local hit,Position = game.Workspace:FindPartOnRayWithIgnoreList(frontRay,{body})
    local crosswalk = game.Workspace.CrswlkObj
    if hit == crosswalk then
        print("Reached Cross walk")
        vs.Throttle = hit.Velocity.Z
        wait(1)
        vs.Throttle = 1 
    end

    --this is the line follower

end)
--ok

1 comment

0

Sorry it does not work. Maybe I might be doing something wrong.

This discussion is preserved as it was. New answers, comments, and votes are closed.