[Standalone] Crutches

I don’t use qb-core, so I’m unable to test it, but I would presume you could to do something like this:

(server-side script)

local QBCore = exports['qb-core']:GetCoreObject()

RegisterCommand("crutches", function(source, args, rawCommand)
    local src = source
	local playerId = args[1]
	local time = args[2]

	if playerId == nil then
		TriggerClientEvent('chat:addMessage', source, { args = { 'PlayerId was invalid' } })
		return
	else
		playerId = tonumber(playerId)
	end

	if time == nil then
		TriggerClientEvent('chat:addMessage', source, { args = { 'Time was invalid' } })
		return
	else
		time = tonumber(time)
	end

    local Player = QBCore.Functions.GetPlayer(src)
    if Player.PlayerData.job.name == 'ambulance' and Player.PlayerData.job.onduty then
        TriggerClientEvent('crutches:forceEquip', playerId, true, time)
    else
        TriggerClientEvent('chat:addMessage', source, { args = { 'You are not allowed to use this command!' } })
    end
end, false)

whats your disc?

im getting not safe for net in f8 when trying it

1 Like

Just add RegisterNetEvent('crutches:forceEquip') above AddEventHandler('crutches:forceEquip', function(state, time) in client.lua so it looks like this:

-- Time is in seconds
RegisterNetEvent('crutches:forceEquip')
AddEventHandler('crutches:forceEquip', function(state, time)

thanks btw how u would u make the character not run or pull out a gun in crutches?

1 Like

The timer doesn’t seem to be working. I set it to 5 seconds and it just continues.

Are you sure you implemented everything correctly? And when you filled out the time parameter, did you enter 5000 or just 5?

just 5

Ok, here is the entire file I use with the examples:

client.lua
-- Config --
local crutchModel = -1035084591 -- v_med_crutch01
local clipSet = "move_lester_CaneUp"
local pickupAnim = {
	dict = "pickup_object",
	name = "pickup_low"
}

local localization = {
	['ragdoll'] = "You can't use a crutch while you are in ragdoll!",
	['falling'] = "You can't use a crutch while you are falling!",
	['combat'] = "You can't use a crutch while you are in combat!",
	['dead'] = "You can't use a crutch while you are dead!",
	['vehicle'] = "You can't use a crutch while you are in a vehicle!",
	['weapon'] = "You can't use a crutch while having a weapon out!",
	['pickup'] = "Press ~INPUT_PICKUP~ to pick up your crutch!"
}

-- Variables --
local isUsingCrutch = false
local crutchObject = nil
local walkStyle = nil
local forceEquipped = false
local endForceTime = 0

-- Functions --
local function LoadClipSet(set)
	RequestClipSet(set)
	while not HasClipSetLoaded(set) do
		Citizen.Wait(10)
	end
end

local function LoadAnimDict(dict)
	RequestAnimDict(dict)
	while not HasAnimDictLoaded(dict) do
		Citizen.Wait(10)
	end
end

local function DisplayNotification(msg)
	BeginTextCommandThefeedPost("STRING")
	AddTextComponentSubstringPlayerName(msg)
	EndTextCommandThefeedPostTicker(false, false)
end

local function DisplayHelpText(msg)
	BeginTextCommandDisplayHelp("STRING")
	AddTextComponentSubstringPlayerName(msg)
	EndTextCommandDisplayHelp(0, false, true, 50)
end

local function CreateCrutch()
	if not HasModelLoaded(crutchModel) then
		RequestModel(crutchModel)
		while not HasModelLoaded(crutchModel) do
			Citizen.Wait(10)
		end
	end
	local playerPed = PlayerPedId()
	crutchObject = CreateObject(crutchModel, GetEntityCoords(playerPed), true, false, false)
	AttachEntityToEntity(crutchObject, playerPed, 70, 1.18, -0.36, -0.20, -20.0, -87.0, -20.0, true, true, false, true, 1, true)
end

local function CanPlayerEquipCrutch()
	local playerPed = PlayerPedId()
	local hasWeapon, weaponHash = GetCurrentPedWeapon(playerPed, true)

	if hasWeapon then
		return false, localization['weapon']
	elseif IsPedInAnyVehicle(playerPed, false) then
		return false, localization['vehicle']
	elseif IsEntityDead(playerPed) then
		return false, localization['dead']
	elseif IsPedInMeleeCombat(playerPed) then
		return false, localization['combat']
	elseif IsPedFalling(playerPed) then
		return false, localization['falling']
	elseif IsPedRagdoll(playerPed) then
		return false, localization['ragdoll']
	end
	return true
end

local function UnequipCrutch()
	if DoesEntityExist(crutchObject) then
		DeleteEntity(crutchObject)
	end

	isUsingCrutch = false
	local playerPed = PlayerPedId()
	
	if walkStyle then
		LoadClipSet(walkStyle)
		SetPedMovementClipset(playerPed, walkStyle, 1.0)
		RemoveClipSet(walkStyle)
	else
		ResetPedMovementClipset(playerPed, 1.0)
	end
end

local function EquipCrutch()
	local playerPed = PlayerPedId()
	local canEquip, msg = CanPlayerEquipCrutch()
	if not canEquip then
		DisplayNotification(msg)
		return
	end

	LoadClipSet(clipSet)
	SetPedMovementClipset(playerPed, clipSet, 1.0)
	RemoveClipSet(clipSet)

	CreateCrutch()
	isUsingCrutch = true

	Citizen.CreateThread(function()
		local fallCount = 0

		while true do
			Citizen.Wait(250)
			if not isUsingCrutch then
				break
			end

			local playerPed = PlayerPedId()
			local isCrutchHidden = false
			local hasWeapon, weaponHash = GetCurrentPedWeapon(playerPed, true)

			if IsPedInAnyVehicle(playerPed, true) or hasWeapon then
				if not isCrutchHidden then
					isCrutchHidden = true
					if DoesEntityExist(crutchObject) then
						DeleteEntity(crutchObject)
					end
				end
			elseif not DoesEntityExist(crutchObject) then
				Citizen.Wait(750)
				CreateCrutch()
				isCrutchHidden = false
			elseif not IsEntityAttachedToEntity(crutchObject, playerPed) then
				local traceObject = true
				while traceObject do
					local wait = 0
					if DoesEntityExist(crutchObject) then
						playerPed = PlayerPedId()
						if not IsPedFalling(playerPed) and not IsPedRagdoll(playerPed) then
							local dist = #(GetEntityCoords(playerPed)-GetEntityCoords(crutchObject))
							if dist < 2.0 then
								DisplayHelpText(localization['pickup'])
								if IsControlJustReleased(0, 38) then
									LoadAnimDict(pickupAnim.dict)
									TaskPlayAnim(playerPed, pickupAnim.dict, pickupAnim.name, 2.0, 2.0, -1, 0, 0, false, false, false)

									local failCount = 0
									while not IsEntityPlayingAnim(playerPed, pickupAnim.dict, pickupAnim.name, 3) and failCount < 25 do
										failCount = failCount + 1
										Citizen.Wait(50)
									end
									if failCount >= 25 then
										ClearPedTasks(playerPed)
									else
										Citizen.Wait(800)
									end

									RemoveAnimDict(pickupAnim.dict)
									DeleteEntity(crutchObject)
									Citizen.Wait(900)
									CreateCrutch()
									traceObject = false
								end
							elseif dist < 200.0 then
								wait = dist * 10
							else
								traceObject = false
							end
						else
							wait = 250
						end
					else
						traceObject = false
					end
					Citizen.Wait(wait)
				end
			elseif IsPedRagdoll(playerPed) or IsEntityDead(playerPed) then
				DetachEntity(crutchObject, true, true)
			elseif IsPedInMeleeCombat(playerPed) then
				Citizen.Wait(400)
				DetachEntity(crutchObject, true, true)
			elseif IsPedFalling(playerPed) then
				fallCount = fallCount + 1
				if fallCount > 3 then
					DetachEntity(crutchObject, true, true)
					fallCount = 0
				end
			elseif fallCount > 0 then
				fallCount = fallCount - 1
			end
		end
	end)
end

local function ToggleCrutch()
	if isUsingCrutch then
		if forceEquipped then
			DisplayNotification("You need to use the Crutch for a little longer!")
			return
		end
		UnequipCrutch()
	else
		EquipCrutch()
	end
end

local function StartForcedTimer(time)
	Citizen.CreateThread(function()
		endForceTime = GetGameTimer() + time * 1000

		while true do
			Citizen.Wait(1000)
			print(endForceTime, GetGameTimer())
			if endForceTime < GetGameTimer() then
				break
			end
		end

		forceEquipped = false
	end)
end

-- Exports --
exports('SetWalkStyle', function(walk)
	walkStyle = walk
end)

-- Commands --
RegisterCommand("crutch", function(source, args, rawCommand)
	ToggleCrutch()
end, false)

-- Events --
-- Time is in seconds
AddEventHandler('crutches:forceEquip', function(state, time)
	forceEquipped = state
	if forceEquipped then
		if not isUsingCrutch then
			EquipCrutch()
		end
		StartForcedTimer(time)
	end
end)

It includes a print, maybe that tells you a little bit about what’s wrong?

yo bro can you send for esx and what scripts i want ems to beable to do command the crutch away and for them to get the crutch when they get revive from ems and ems only like revivep

is there a way to make the crutches say on and not fall off until you finish being in it i want it to be stuck until the timer is done

like i want the crutches to be forced and stay on , also the walking style until the timer is over

Found this because someone is selling something similar. Thank you very much for releasing this open source.

You should make it so can’t run pull out weapons etc.

Thanks for the suggestion, I’ll see if I can’t add it!

Update!

Refactored some code and added some requested features.

Changelog 1.1.2

  • Added the examples of forcing crutches on people directly into the script.
  • Added option to disable weapons while using a crutch (yet to be tested with an inventory, but should hopefully work).
  • Added option to disable sprinting.
  • Disabled default idle anim while using a crutch. This should stop the standing up that happens every couple of seconds when not moving.
  • Splitt up some functions to make it easier to read the code.
1 Like

how do i join the server

how can i made only ems can remove the crutch completely because you can only reduce or set to 00 time but they keep the crutch i want only the ems be available to remove it

What server to be exact?

Depending on what job system you use and how you want it, you could for example use the ace system to restrict the command and give permissions to ems people, or you could check for their job when they execute the command. It really depends.