Shift lock Script does not work properly

for some reason the script wont run after creating ContextActionService with the TouchButton as true.

normal play test:

client and server play test

Code that wont run
ContextActionService:BindAction(ACTION_NAME, OnShiftLock, true)

local ActionButton: ImageButton = ContextActionService:GetButton(ACTION_NAME)

print(ActionButton)

repeat 
	task.wait()
	ActionButton = ContextActionService:GetButton(ACTION_NAME)
	print("ssss")
until ActionButton ~= nil

print(ActionButton)

ActionButton.AnchorPoint = Vector2.new(0.5, 0.5)
ActionButton.Position = UDim2.fromScale(0.94, 0.89)
ActionButton.Size = UDim2.fromScale(0.12, 0.17)
ActionButton.Image = disabled_Image_ID

while task.wait() do
	if ActionButton then
		print("sss")
		ActionButton.Image = if ACTIVED then enabled_Image_ID else disabled_Image_ID
	end
end
full code
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local ACTION_NAME = "ShiftLock"

local LocalPlayer = Players.LocalPlayer
local CurrentCamera = workspace.CurrentCamera

local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:FindFirstChild("Humanoid")
local RootPart: BasePart = Character:FindFirstChild("HumanoidRootPart")

local ACTIVED: RBXScriptConnection = nil
local ENABLED_OFFSET = CFrame.new(1.7, 0, 0)
local DISABLED_OFFSET = CFrame.new(-1.7, 0, 0)

local enabled_Image_ID = "rbxassetid://129283493733506"
local disabled_Image_ID = "rbxassetid://133377395107120"

local function GetUpdatedCameraCFrame(ROOT: BasePart, CAMERA: Camera)
	local MAX_LENGTH = 900000
	return CFrame.new(ROOT.Position, Vector3.new(CAMERA.CFrame.LookVector.X * MAX_LENGTH, ROOT.Position.Y, CAMERA.CFrame.LookVector.Z * MAX_LENGTH))
end

local function EnableShiftlock()
	Humanoid.AutoRotate = false
	RootPart.CFrame = GetUpdatedCameraCFrame(RootPart, CurrentCamera)
	CurrentCamera.CFrame = CurrentCamera.CFrame * ENABLED_OFFSET
end

local function DisableShiftlock()
	Humanoid.AutoRotate = true
	CurrentCamera.CFrame = CurrentCamera.CFrame * DISABLED_OFFSET
	
	local Success, ErrorMsg = pcall(function()
		ACTIVED:Disconnect()
		ACTIVED = nil
	end)
	
	if not Success then warn(ErrorMsg) end
end

function OnShiftLock(actionName: string, inputState: Enum.UserInputState, inputObject: InputObject)
	if inputState ~= Enum.UserInputState.Begin then return end
	if actionName == ACTION_NAME then
		if not ACTIVED then
			ACTIVED = RunService.RenderStepped:Connect(EnableShiftlock)
		else
			DisableShiftlock()
		end
	end
end

ContextActionService:BindAction(ACTION_NAME, OnShiftLock, true)

local ActionButton: ImageButton = ContextActionService:GetButton(ACTION_NAME)

print(ActionButton)

repeat 
	task.wait()
	ActionButton = ContextActionService:GetButton(ACTION_NAME)
	print("ssss")
until ActionButton ~= nil

print(ActionButton)

ActionButton.AnchorPoint = Vector2.new(0.5, 0.5)
ActionButton.Position = UDim2.fromScale(0.94, 0.89)
ActionButton.Size = UDim2.fromScale(0.12, 0.17)
ActionButton.Image = disabled_Image_ID

while task.wait() do
	if ActionButton then
		print("sss")
		ActionButton.Image = if ACTIVED then enabled_Image_ID else disabled_Image_ID
	end
end
1 Like

What’s happening is the GetButton line infinitely yields because the ContextActionService Gui isn’t parented to PlayerGui quick enough which is odd to say the least. In order to fix this you need to wait for the ContextActionService Gui to be parented to the PlayerGui.

Here’s the code to fix it, you can put this under the bind action line:

local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
local ContextActionServiceGui = PlayerGui:WaitForChild("ContextActionGui")
2 Likes

This worked but i decided to remove the ContextActionService and use the GUI instead but thank you

2 Likes

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