ContextActionService:BindAction can only be called from a local script Issue

I was in the process of making a Mobile Button for a light switch

No matter what I tried it kept appearing in the logs that it can only be called in a local script. I’ve tried re-writing the script and making another localscript

---[{Vars}]
local LocalPlayer = game:GetService("Players").LocalPlayer
local CAS = game:GetService("ContextActionService")
local UIS = game:GetService("UserInputService")
local ClickSoundEFFECT = script.Parent.F_FlashLight:WaitForChild("FlashLightClick")
local Head = game.Players.LocalPlayer.Character:WaitForChild("Head")
local FlashLight = Head:WaitForChild("SurfaceLight")
local FlashLightState = game.ReplicatedStorage.FlashLightState
local GS = game:GetService("GuiService")

---[{Functions}]
function FlashlightOn()
	FlashLight.Enabled = true
end

function FlashlightOff()
	FlashLight.Enabled = false
end

function FullScript()
	if FlashLightState.Value == true then
		FlashLightState.Value = false
		ClickSoundEFFECT:Play()
		FlashlightOn()
	else
		FlashLightState.Value = true
		ClickSoundEFFECT:Play()
		FlashlightOff()
	end
end

---[{Finds Player's Platforms}]
function getPlatform()
	if (GS:IsTenFootInterface()) then
		return "Console"
	elseif (UIS.TouchEnabled and not UIS.KeyboardEnabled) then
		return "Mobile"
	else
		return "Desktop"
	end
end

---[{Don't Mess With Anything Here}]

if getPlatform() == "Mobile" then
	CAS:BindAction("TurnOnAndOffFlashLight", FullScript, true, "f" )
end

I’m not sure what exactly I did wrong.

So is your script a Local Script?
It has to be for you to ContextActionService or UserInputService

Edit: Apparently I’m blind disregard this

Where is the LocalScript?
As stated here: LocalScript

I’m assuming it’s just the location of the script.

It’s in "StarterPlayerScripts I’m not sure whats wrong

I see that you’re trying to reference the Character as well, you could try moving it to StarterPlayer > StarterCharacterScripts

So that the Character is ready right away. Not sure if moving it would fix the problem.

Are there any other errors?

1 Like

I moved it to someplace else it finally got it to work. Sorry for wasting your time or something on the lines of that

3 Likes