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

My hover car code is not working and I can't find out what's wrong with it?


local NullVector = Vector3.new(0,0,0)
local player = game.Players.LocalPlayer
local speed = 20
local rotend = true
local moveend = true
local pos = script.Parent.CarStat.Value
local rot = pos.Parent.BodyAngularVelocity
local uis = game:GetService("UserInputService")
uis.InputBegan:connect(function(input)
	local keycode = input.KeyCode
	if keycode == Enum.KeyCode.W then
		moveend = true
		moveend = false
		pos.Velocity = pos.Parent.CFrame.lookVector*speed
		repeat wait() until moveend == true
		pos.Velocity = NullVector
	end
	if keycode == Enum.KeyCode.S then
		moveend = true
		moveend = false
		pos.Velocity = (pos.Parent.CFrame.lookVector*-1)*speed
		repeat wait() until moveend == true
		pos.Velocity = NullVector
	end
	if keycode == Enum.KeyCode.A then
		rotend = true
		rotend = false
		rot.AngularVelocity = Vector3.new(0,-10,0)
		repeat wait() until rotend == true
		rot.AngularVelocity = NullVector
	end
	if keycode == Enum.KeyCode.D then
		rotend = true
		rotend = false
		rot.AngularVelocity = Vector3.new(0,10,0)
		repeat wait() until rotend == true
		rot.AngularVelocity = NullVector
	end
end)

uis.InputEnded:connect(function(input)
	local key = input.KeyCode
	if key == Enum.KeyCode.W or key == Enum.KeyCode.S then
		moveend = true
	end
	if key == Enum.KeyCode.A or key == Enum.KeyCode.D then
		rotend = true
	end
end)

I already made the script that turns the car on when you sit in it. This localscript is put into the player that is sitting in the car. That works just fine but the Localscript (above) is not working nor is it raising an error to help me debug the problem. I need someone else who can easily spot errors to help me. I have no clue what's wrong.

1 answer

✓ Accepted answer

For the future, one method of figuring out what's wrong is to start adding 'print' statements in various places. ex, maybe you set up the InputBegan incorrectly? You might put a print statement on line 10 or 11 and print out first that it's running, and then second the value of 'input' and/or 'input.KeyCode'.

In this case, I can spot one problem, though it isn't "the" problem:

You assign "moveend = true" and then immediately assign "moveend = false". This doesn't work because lua only runs one thing at a time, and it doesn't switch to other coroutines/threads until it hits the end of the function or a 'wait' command (or similar). Your intent seems to be to release the previous coroutine from its "repeat wait() until moveend == true" loop, but you'd need a wait() command to give the repeat loop a chance to consider exiting.

A better method is to simply move the pos.Velocity = NullVector code into the InputEnded event and delete moveend/rotend entirely.

Now, if the script is working correctly (which it may very well be doing right now, as my proposed change doesn't fundamentally change what it's doing), you should check to make sure that the "CarStat.Value" is changing correctly (test it in Solo mode and find it in Explorer. Check the Properties window to make sure the value is changing). If it is changing correctly, you know your script is fine - the problem is that whatever is supposed to be reading from 'CarStat.Value' is having problems.

1 comment

0

This answer didn't tell me what the problem was but it did help me find it.

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