Press C to roll

How would I make it so when I press C to roll, for example when I would press C I would get a slight boost that pushes you forward. When you press the button a animation would also play. How would I also make a cooldown between each “roll”?

I already have the animation done for it. This game is also in R6

4 Likes

look into this:

then, for the cooldown use a debounce, like

local canRoll = true
WhenCPressed:Connect(function()
     if not canRoll then return end
     rollAnimation:Play()
     canRoll = false
     wait(3)
     canRoll = true
end)
2 Likes

You will also want to look in to bodyVelocity, this will make your character move forward when they press the desired button and use UserInputService.
This is how you would get C

UserInputService works great for getting keyboard actions.
BodyVelocity

Setting the bodyVelocity to your lookVector (CFrame | Documentation - Roblox Creator Hub) will make your player roll where ever they are looking,

I don’t understand in the body velocity how it would push me forward or where it says it. And by looking do you mean where their mouse is or where their body is facing?

BodyVelocity basically is an unseen force (like gravity) that pushes against a basepart.

So I’ll show you a demo script(Read the notes, they will help you understand)

local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local function Roll(Player) -- You would get the Player from firing of a remoteEvent
local Root = game.Player.Character:WaitForChild("HumanoidRootPart") -- Your Characters torso
local BV = instance.new("BodyVelocity") -- Making the Force
BV.MaxForce = Vector3.New(math.Huge, math.Huge, math.Huge) -- Don't worry about this
BV.Velocity = Root.CFrame.LookVector * 90 -- The number is the speed, LookVector gets where the part is facing
BV.Parent = Root -- Puts the force in the Torso
delay(0.5, function()
BV:Destroy() -- Removes Bv so character doesn't get pushed forever(aka. how long the dash is)
end)
end)
RemoteEvent.OnServerEvent:Connect(Roll) -- You have a remoteEvent fire from a local script(This will be where your UserInputService is at)
1 Like

How would I put that into the roll script though? I’m guessing this is how far it woudl push you.

BV.Velocity = Root.CFrame.LookVector * 90

This is quite the old post, but I was also figuring out how to make a roll script. After a bit of time, I figured out “most” how to do it. So let me share my process “step by step” so others can see. I’m not very experienced so correct me if I’m wrong. It’s part of the learning process :slight_smile:

THE FULL SCRIPT WILL BE AT THE BOTTOM

I will be using only a local script as it is more simple to do.

First, the OP asked,

Let’s figure out how to do inputs. I still don’t understand ContextActionServices that well, so I’ll just be using UserInputService, which will help us get our input, C.

local UIS = game:GetService("UserInputService")

Now we need to detect an input from userInputService by using InputBegan and then connecting to the function with parameters “input”.

UIS.InputBegan:Connect(function(input, gameProcessed)	
	if not gameProcessed then

	end
end)

Then we need to check “if the key pressed was c” by using an if statement.

UIS.InputBegan:Connect(function(input, gameProcessed)	
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.C then

		end
	end
end)

Now let’s make the function to make the player roll. I’ll be taking Straight’s code

and making some modifications. I’ll be adding animation also

local LocalPlayer = game:GetService("Players").LocalPlayer
local chr = LocalPlayer.Character
local Debris = game:GetService("Debris")
local humanoid = chr.Humanoid

local rollTime = 1
local rollMagnitude = 50

local animation = Instance.new("Animation")
animation.AnimationId = "http://www.roblox.com/asset/?id=507771019" -- Roblox dance emote
local animationTrack = humanoid:LoadAnimation(animation)

function createBodyForce()
    animationTrack:Play()
	local rT = chr:WaitForChild("HumanoidRootPart") 
	local bV = Instance.new("BodyVelocity") 
	
	bV.MaxForce = Vector3.new(math.huge, 0, math.huge) 
	bV.Velocity = rT.CFrame.LookVector * rollMagnitude 
	bV.Parent = rT
	
    delay(rollTime , function()
        animationTrack:Stop()
        Debris:AddItem(bV, 0)
    end)
end

The major changes in this modification is that the roll is now affected by gravity, and that animation plays.

Next, let’s make the roll cooldown.

local canRoll = true

function roll(cD) --the cooldown time for roll
	if canRoll then
		canRoll = false
		createBodyForce()
		
		task.wait(cD)
		
		canRoll = true
	end
end

After that, call the roll function when C is pressed.

UIS.InputBegan:Connect(function(input, gameProcessed)	
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.C then
		    roll(10)
		end
	end
end)

That’s basically all, here’s the cleaned up script below. (With extra stuff)

local UIS = game:GetService("UserInputService")
local Debris = game:GetService("Debris")
local LocalPlayer = game:GetService("Players").LocalPlayer
local chr = LocalPlayer.Character
local humanoid = chr.Humanoid

local rollTime = 0.5
local rollMagnitude = 40
local canRoll = true

local animation = Instance.new("Animation")

if chr:FindFirstChild("UpperTorso") then
	animation.AnimationId = "0" --R15 Animation
else
	animation.AnimationId = "0" --R6 Animation
end

local animationTrack = humanoid:LoadAnimation(animation)

local function isRotation(value)
	humanoid.AutoRotate = value --stops player from rotatiing
end

UIS.InputBegan:Connect(function(input, gameProcessed)	
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.C then
		    roll(10)
		end
	end
end)

function createBodyForce()
	isRotation(false)
	local speed = animationTrack.Length / rollTime

	animationTrack:Play()
	animationTrack:AdjustSpeed(speed)
	
	local rT = chr:WaitForChild("HumanoidRootPart") 
	local bV = Instance.new("BodyVelocity") 

	bV.MaxForce = Vector3.new(math.huge, 0, math.huge) 
	bV.Velocity = rT.CFrame.LookVector * rollMagnitude 
	bV.Parent = rT

	delay(rollTime , function()
		isRotation(true)
		animationTrack:Stop()
		Debris:AddItem(bV, 0)
	end)
end

function roll(cD) --the cooldown time for roll
	if canRoll and humanoid:GetState() == Enum.HumanoidStateType.Running then
		canRoll = false
		createBodyForce()

		task.wait(cD + rollTime)

		canRoll = true
	end
end

Not the best roll script, but it does it’s best

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
   if not gameProcessedEvent then
      return
   end

   -- ...
end

?

1 Like

never even heard of gameProcessedEvent, shouldve used that.

1 Like