What would a way to improve my sphere rolling system?

https://www.roblox.com/games/6848012644/MYSTERY-MEN-THAT-ARE-OBJECTS-KNOWN-AS-A-SPHERE

I have created a sphere rolling system inside my game and I would like to know, how are some ways or methods I could incorpate to make the spheres roll smoother and change their direction faster or easier?

The script used to make the sphere roll is ran on the client being;

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local ball = script.Parent.Head
local camera = workspace.CurrentCamera

camera.CameraSubject = ball

local WHeld = false
local AHeld = false
local SHeld = false
local DHeld = false

local Tween = nil

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local KeyPressed = input.KeyCode
		if KeyPressed == Enum.KeyCode.W or KeyPressed == Enum.KeyCode.A or KeyPressed == Enum.KeyCode.S or KeyPressed == Enum.KeyCode.D then
			if Tween ~= nil then
				Tween:Cancel()
				Tween = nil
				print("Cancelled")
			end
		end
		if KeyPressed == Enum.KeyCode.W then
			coroutine.resume(coroutine.create(function()
				WHeld = true
				repeat
					wait(0.01)
					ball.BodyForce.Force = ball.BodyForce.Force + (camera.CFrame.LookVector * Vector3.new(15,0,15))
				until WHeld == false
			end))
		end
		if KeyPressed == Enum.KeyCode.A then
			coroutine.resume(coroutine.create(function()
				AHeld = true
				repeat
					wait(0.01)
					ball.BodyForce.Force = ball.BodyForce.Force + (camera.CFrame.rightVector * Vector3.new(-15,0,-15))
				until AHeld == false
			end))
		end
		if KeyPressed == Enum.KeyCode.S then
			coroutine.resume(coroutine.create(function()
				SHeld = true
				repeat
					wait(0.01)
					ball.BodyForce.Force = ball.BodyForce.Force + (camera.CFrame.LookVector * Vector3.new(-15,0,-15))
				until SHeld == false
			end))
		end
		if KeyPressed == Enum.KeyCode.D then
			coroutine.resume(coroutine.create(function()
				DHeld = true
				repeat
					wait(0.01)
					ball.BodyForce.Force = ball.BodyForce.Force + (camera.CFrame.rightVector * Vector3.new(15,0,15))
				until DHeld == false
			end))
		end
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		local KeyPressed = input.KeyCode
		if KeyPressed == Enum.KeyCode.W then
			WHeld = false
			ball.BodyForce.Force = Vector3.new(0,0,0)
		end
		if KeyPressed == Enum.KeyCode.A then
			AHeld = false
			ball.BodyForce.Force = Vector3.new(0,0,0)
		end
		if KeyPressed == Enum.KeyCode.S then
			SHeld = false
			ball.BodyForce.Force = Vector3.new(0,0,0)
		end
		if KeyPressed == Enum.KeyCode.D then
			DHeld = false
			ball.BodyForce.Force = Vector3.new(0,0,0)
		end
		
		if WHeld == false and AHeld == false and SHeld == false and DHeld == false then
			if KeyPressed == Enum.KeyCode.W or KeyPressed == Enum.KeyCode.A or KeyPressed == Enum.KeyCode.S or KeyPressed == Enum.KeyCode.D then
				Tween = TweenService:Create(
					ball,
					TweenInfo.new(1),
					{Velocity = Vector3.new(0,0,0),RotVelocity = Vector3.new(0,0,0)}
				)
				
				Tween:Play()
				ball.BodyForce.Force = Vector3.new(0,0,0)
				print("k")
			end
		end
	end
end)

Any general feedback would also be appricated

2 Likes

Alrighty couple ways first of all

userinputservice has a function instead of all that work to detect held do

if UserInputService:IsKeyDown(Enum.KeyCode.W) then

end

which is better and try to keep coroutines to a minimuim