How to make the same key do something else?

I’m trying to make an R to Ragdoll script but one problem, I want it to be the same button to be pressed in order to ragdoll and unragdoll, so how do I do that?

2 Likes

You can create a boolean in the script and set it to false at first, and when you press R do

 boolean = not boolean

so when you press R to ragdoll, the boolean will be true, and from there you can do an if check to do something based on the boolean
so if boolean then ragdoll, elseif not boolean then unragdoll.

1 Like

this should be a localscript in startercharacterscripts

local ragdolled = false
local CAS = game:GetService("ContextActionService")
local createMobileButton = false --true if you want a button for mobile

function ragdoll()
   --ragdoll
end

function unragdoll()
   --unragdoll
end

function manageRagdoll()
   ragdolled = not ragdolled
   if ragdolled then ragdoll() else unragdoll() end
end

CAS:BindAction("Ragdoll", manageRagdoll, createMobileButton, Enum.KeyCode.R)
1 Like

if humanoid:GetState() == Enum.HumanoidStateType.Ragdoll then
humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
else
humanoid:ChageState(Enum.HumanoidStateType.Ragdoll)
end

1 Like

Hey, Thanks so much! It works I totally forgot about CAS

2 Likes

this wouldnt work since ChangeState(Ragdoll) needs to be in a string form ChangeState("Ragdoll") and same with "GettingUp"

there I fixed it by using Enum.HumanoidStateType

1 Like

it doesn’t really matter since he is using a custom ragdoll and not roblox’s default

I didnt know that and he didnt mention it in his initial post

1 Like

yeah, I just know because of his most recent topic

1 Like

Testing for when the R key is released and using a variable to hold state.

local UserInputService = game:GetService("UserInputService")
local Ragdoll = false

local function onKeyRelease(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.R and not gameProcessedEvent then
		if Ragdoll == false then Ragdoll = true
		else Ragdoll = false	
		end
		
		print(Ragdoll)
	end
end

UserInputService.InputEnded:Connect(onKeyRelease)

decided to shorten it cuz why not

local CAS = game:GetService("ContextActionService")
local Ragdoll = false

CAS:BindAction("ToggleRagdoll", function() Ragdoll = not Ragdoll print(tostring(Ragdoll)) end, false, Enum.KeyCode.R)
1 Like

Sure, just showing how to use a variable to keep track. I like to use released rather than press, It is just less error prone.