How to decrease RotVelocity while not holding a button

Hello!
I have script which lets player control a ball, but i have issues with making script slowly decreasing RotVelocity speed.
Here it is:

InputService.InputEnded:Connect(function()
	print "stop"
	if not InputService:IsKeyDown('W') or not InputService:IsKeyDown('A') or not InputService:IsKeyDown('S') or not InputService:IsKeyDown('D') then 
		while wait() do
		Ball.RotVelocity = Ball.RotVelocity * 0.95
		print "stop2"
		wait(0.2)
		end
	end
end)

issue is that i dont know how to make it stop script after RotVelocity will be 0.
And, if i can ask, how can i set limit for RotVelecoty? Thanks for your time, let me know if you need any more info.

2 Likes

you can set a cap/min to a value by doing this
Ball.RotVelocity = math.clamp(Ball.RotVelocity * 0.95,0,100)

in math.clamp(x,min,max) , x is the value you want to clamp . min is the minimum the value to be and max is the maximum the value can be.

1 Like

thanks for help with this issue and explanation, have a nice day!

1 Like

if it works, please press the solution button ! It would help other people that face the same problem find a solution easily! Thank you :smiley:

1 Like

Hello! I think i might be trying to put it in the wrong place because it doesnt want to work. Ill send whole script, maybe script itself is blocking something.

local Player = game:GetService('Players').LocalPlayer;
local InputService = game:GetService('UserInputService');
local Camera = workspace.CurrentCamera;
local Settings = {
	Speed = 10,
	JumpPower = 60,
	Size = Vector3.new(5, 5, 5)
}

local Character = Player.Character or Player.CharacterAdded:Wait();
local Humanoid = Character:WaitForChild('Humanoid');
local RayParams = RaycastParams.new();
local Ball = Character:WaitForChild('Ball');
local Connection;
Humanoid.PlatformStand = true

Camera.CameraSubject = Ball
RayParams.FilterType = Enum.RaycastFilterType.Blacklist;
RayParams.FilterDescendantsInstances = {Character};

for _, v in next, Player.Character:GetChildren() do
	if v:IsA('BasePart') and v ~= Ball then
		v.Massless = true;
		v.CanCollide = false;
	end
end



Humanoid.Died:Connect(function() Connection:Disconnect() end);
Connection = game:GetService('RunService').RenderStepped:Connect(function(delta)
	Humanoid.PlatformStand = true;
	Ball.CanCollide = true;

	
	if InputService:IsKeyDown('W') then
		Ball.RotVelocity -= Camera.CFrame.RightVector * delta * Settings.Speed
	end

	if InputService:IsKeyDown('A') then
		Ball.RotVelocity -= Camera.CFrame.LookVector * delta * Settings.Speed
	end

	if InputService:IsKeyDown('S') then
		Ball.RotVelocity += Camera.CFrame.RightVector * delta * Settings.Speed
	end

	if InputService:IsKeyDown('D') then
		Ball.RotVelocity += Camera.CFrame.LookVector * delta * Settings.Speed
	end
		

end)

InputService.JumpRequest:Connect(function()
	if workspace:Raycast(Ball.Position, Vector3.new(0, -((Ball.Size.Y / 2) + 0.3), 0), RayParams) then
		Ball.Velocity = Ball.Velocity + Vector3.new(0, Settings.JumpPower, 0)
	end
end)

InputService.InputEnded:Connect(function()
	print "stop"
	if not InputService:IsKeyDown('W') or not InputService:IsKeyDown('A') or not InputService:IsKeyDown('S') or not InputService:IsKeyDown('D') then 
		while wait() do
		Ball.RotVelocity = Ball.RotVelocity * 0.95
		print "wait2"
		wait(0.2)
		end
	end
end)

Ball.RotVelocity = math.clamp(Ball.RotVelocity * 0.95,0,100)


Hi I am currently typing this on mobile so there might be typos

basically, in this function, you can add this line to the end

local x,y,z = Ball.RotVelocity.X , Ball.RotVelocity.Y , Ball.RotVelocity.Z
Ball.RotVelocity = math.clamp(x,-200,200),math.clamp(u,-200,200),math.clamp(z,-200,200)

how this works is that the RotVelocity is a vector3 that has 3 number which are x,y and z. I take the individual component of each coordinate and clamp it to -200 and 200 . Why -200 you might ask? cuz you would want to lock the min speed of the ball as the negative side of the max speed as you would want the ball to be able to spin backwards also . I recommend setting 200 as a variable and change the clamp code to something like this

math.clamp(x ,-MaxSpeed,MaxSpeed)
1 Like

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