Bodyvelocity.Velocity not changing

So I made a cart for a cart ride game (you may remember an earlier post about it) and I tried to make the speed change whenever you click some buttons, the stop button also will not work. This is the script:

local Model = script.Parent
local Main = Model.Main
local speed = 25

function go()
	if Model.Engine ~= nil then
		local bv = Instance.new("BodyVelocity",Main)
		bv.MaxForce = Vector3.new(10000,10000,10000)
		while true do
			bv.Velocity = Main.CFrame.LookVector  * speed
			wait(0.1)
		end
		
	end
	
end

Model.Go.ClickDetector.MouseClick:Connect(go)

function stop()
	if Model.Engine ~= nil then
		speed = 0
	end

end

Model.Stop.ClickDetector.MouseClick:Connect(go)

function faster()
	if Model.Engine ~= nil then
		if speed ~= 0 then
			speed = speed + 5
		end
	end

end

Model.Faster.ClickDetector.MouseClick:Connect(go)

function slower()
	if Model.Engine ~= nil then
		if speed ~= 5 or speed ~= 0 then
			speed = speed - 5
		end
	end

end

Model.Slower.ClickDetector.MouseClick:Connect(go)

while true do
	local fuel = Model.Fuel
	if speed > 0 then
		wait(2)
		fuel.Value = fuel.Value - speed * 5
	end
end

I am changing the speed value and then the while loop should automatically changes the velocity but it does not seem to be doing anything. The start button works but the stop button and speed buttons do not work.

If you know how I can fix this, please let me know.

1 Like

The code seems to work for me.

if Model.Engine ~= nil then --are you sure it's getting past this if statement? use print() to check

Only thing i modified was the parenting:

local bv = Instance.new("BodyVelocity")
bv.Parent = Main --Parent it like this instead, the second parameter of Instance.new() is deprecated
bv.MaxForce = Vector3.new(10000,10000,10000)
while true do
	bv.Velocity = Main.CFrame.LookVector  * speed
	wait(0.1)
end

This did not seem to fix anything. It also seems as you don’t really see the problem. Would you like a video of what is not working?

edit: I just realized that it may be hard for you to say “Yes” as there are multiple characters needed, so incase you want to see the video here it is:
robloxapp-20221117-1349289.wmv (3.4 MB)
(I hope this is the right one lol)

1 Like

There are some HUGE issues with your code, I’m sorry.
Let me redo your code for you.

local Model = script.Parent
local Main = Model.Main
local Fuel = Model.Fuel
local speed = 25

-- This line is called alot, so it should be a function.
local function isEngine()
    return Model.Engine ~= nil
end


-- Function is only called by one thing, don't need it as a variable.
local shouldGo = false
Model.Go.ClickDetector.MouseClick:Connect(function()
    if not isEngine() then return end
    local bv = Instance.New("BodyVelocity")
    bv.MaxForce = Vector3.new(10000,10000,10000)

    shouldGo = true
    while shouldGo do
        bv.Velocity = Main.CFrame.LookVector * speed
        task.wait(.1)
    end
end)

Model.Stop.ClickDetector.MouseClick:Connect(function() 
    shouldGo = false
end)

Model.Faster.ClickDetector.MouseClick:Connect(function()
    if not isEngine() then return end
    speed += 5
end)

Model.Slower.ClickDetector.MouseClick:Connect(function()
    if not isEngine() then return end
    speed -= 5
end)

while isEngine() do
    if speed ~= 0 then
        fuel.Value -= (speed * 5)
        task.wait(2)
    end
end

Just found your issue only a 1/3 of the way through, you forgot to replace the go when you called it in the other functions.

If you want me to explain anything, let me know.

This did help organize my code better, but I have the same issue.

I hate to be that guy but there’s a two problems i can see with your code:

while isEngine() do
    if speed ~= 0 then --if speed is equal to 0 the loop will run without any wait, causing a crash, just add a else task.wait()
        fuel.Value -= (speed * 5)
        task.wait(2)
    end
end

Also i’m not sure but i think OP wanted the check for the instance’s existance, so it needs to be FindFirstChild, again, i’m not sure if that’s what he meant on thoses if statements.

local function isEngine()
    return Model.Engine ~= nil
end
1 Like

The fuel part which you don’t know why it is there is not complete. It will be making the cart stop when the fuel runs out, i just have not made that yet.

Does this code work for you?

local runService = game:GetService("RunService")

local Model = script.Parent
local Main = Model.Main
local speed = 25

local fuel = Model.Fuel

local maxSpeed = 100
local minSpeed = 5

local lVelocity = nil

local lastTick = tick()
runService.Heartbeat:Connect(function() -- runs every heartbeat
	if lVelocity then
		lVelocity.VectorVelocity = Main.CFrame.LookVector * speed
	end
	
	if tick() - lastTick >= 2 then --using tick instead of loops to check
		if speed > 0 and isEngine() then
			fuel.Value -= speed * 5
		end
		
		lastTick = tick()
	end
end)

function SetSpeed(newSpeed)
	speed = math.clamp(newSpeed, minSpeed, maxSpeed) --sets the speed while respecting the minimum and maximum values
end

function isEngine()
	--return Model:FindFirstChild("Engine")
	return Model.Engine ~= nil
end

function Go()
	if not lVelocity then
		lVelocity = Instance.new("LinearVelocity") --using LinearVelocity instead as BodyVelocity is deprecated
		lVelocity.MaxForce = 10000
		lVelocity.Parent = Main
		
		local attachment = Instance.new("Attachment")
		attachment.Parent = Main
		
		lVelocity.Attachment0 = attachment
		
		SetSpeed(5)
	else
		lVelocity.Enabled = true
	end
end


function Stop()
	if lVelocity then
		lVelocity.Enabled = false
	end
end


function Faster()
	SetSpeed(speed + 5)
end


function Slower()
	SetSpeed(speed - 5)
end

Model.Go.ClickDetector.MouseClick:Connect(Go)
Model.Stop.ClickDetector.MouseClick:Connect(Stop)
Model.Faster.ClickDetector.MouseClick:Connect(Faster)
Model.Slower.ClickDetector.MouseClick:Connect(Slower)

I would much rather do this with classes/components as they are perfect for your case, but learn them at your time, hopefully this code works for you.

1 Like

This works! thank you for helping and sorry for the late response lol.

1 Like

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