Module code issue for self driving car

The whole point of the following code is to give values to the variables I expressed above, they’re not shown here but this is what it leads to:
local function Target_Variables() local target = vehicle.target if typeof(target) == "Instance" and target:FindFirstChild("Humanoid") then local CF = seat.CFrame:ToObjectSpace(target.PrimaryPart.CFrame) if CF.X < 0 then vehicle.target_left = true vehicle.target_right = false else vehicle.target_left = false vehicle.target_right = true end if CF.Z < 0 then vehicle.target_back = true vehicle.target_front = false else vehicle.target_back = false vehicle.target_front = true end end end print("Made it through") game:GetService("RunService").Heartbeat:Connect(Target_Variables) return vehicle
This is supposed to find and move to the player with code established in another script, but I’m getting a bit of nothing really. Help would be much appreciated.

Try adding warnings. Something like so:

local function Target_Variables()
	if vehicle then
		local target = vehicle.target
		if target then
			if typeof(target) == "Instance" then
				if target:FindFirstChild("Humanoid") then
					if seat then

						local CF = seat.CFrame:ToObjectSpace(target.PrimaryPart.CFrame)

						if CF then

							if CF.X < 0 then vehicle.target_left = true vehicle.target_right = false
							else vehicle.target_left = false vehicle.target_right = true
							end
							
							if CF.Z < 0 then vehicle.target_back = true vehicle.target_front = false
							else vehicle.target_back = false vehicle.target_front = true
							end						
						else
							warn("No CF")
						end


					else
						warn("No seat")
					end
				else
					warn("No humanoid")
				end
			else
				warn("Not an Instance")
			end
		else
			warn ("No target")
		end
	else
		warn("No vehicle")
	end
end

print("Made it through") game:GetService("RunService").Heartbeat:Connect(Target_Variables) return vehicle

you should probably use guard clauses instead

example:

function checkForSomething(test)

  if test:IsA("Humanoid") == false then
    warn("not humanoid")
    return
  end

   if test >= 5 then
    warn("too big")
    return
  end

  --- do stuff here

end

It didn’t trip any of the warning messages, so I’m assuming everything is working but isn’t being carried out, because the methods in the module aren’t running even when I specify them in-code.

Then try adding a print statement at the beginning of the function.

Something like so:

local function Target_Variables()
	
	print("Taget Variables Active")
	
	if vehicle then
		local target = vehicle.target
		if target then
			if typeof(target) == "Instance" then
				if target:FindFirstChild("Humanoid") then
					if seat then

						local CF = seat.CFrame:ToObjectSpace(target.PrimaryPart.CFrame)

						if CF then
							
							if CF.X < 0 then vehicle.target_left = true vehicle.target_right = false
							else vehicle.target_left = false vehicle.target_right = true
							end

							if CF.Z < 0 then vehicle.target_back = true vehicle.target_front = false
							else vehicle.target_back = false vehicle.target_front = true
							end						
						else
							warn("No CF")
						end

					else
						warn("No seat")
					end
				else
					warn("No humanoid")
				end
			else
				warn("Not an Instance")
			end
		else
			warn ("No target")
		end
	else
		warn("No vehicle")
	end
end

print("Made it through") game:GetService("RunService").Heartbeat:Connect(Target_Variables) return vehicle

Also, make sure you are calling the function:

Target_Variables()

Sometimes I use returns instead of just letting the function fade.

Is there some reason guards would work better in this instance?

guards are an alternative to if statements

they do the same thing but you only have to indent once, so its much easier to read and change in the future

2 Likes

I am confused.

How is what you provided an alternative to if statements? It uses if statements…

I believe he means that it is an alternative to wrapping a whole code segment with an if statement.

2 Likes

Oh, I see. Thank you. It seems like “guard clauses” would be the better way (and probably standard way) to go in almost any case.

the car moves but i had to completely overhaul the script and remove things from the module script, it also doesnt follow me properly. Ill mark this as the solution and continue the problem on another page.

1 Like

Scratch this, i solved it actually

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.