ContextActionService not working

I am working a build system for my game but ContextActionService just wont work.
It wont print anything when i press E not even an error.

Here is the problematic code.

local ContextActionService = game:GetService("ContextActionService")

function handleAction(actionName, inputState, inputObject)
    if actionName == "PlaceObject" and inputState == Enum.UserInputState.Begin then
        print("Test")
    end
end

ContextActionService:BindAction("PlaceObject", handleAction, true, Enum.KeyCode.E)

I am really not sure what I did wrong here, I have been trying to fix this for the last 3 hours.

1 Like

What type of script is this in and where is the script located in.

Local script and its in starterpack

is the script disabled or something? Cuz I literally just threw this in a script and it worked perfectly fine?

Is Archivable set to true or false?

Its set to true, why would this affect things?

Archivable means it can be cloned > The starterpack isnt where tools/scripts are for the player > they get cloned into the players backpack. If it was false it wouldnt be able to clone over.

Which one that doesn’t work is it “handleAction” or :BindAction?

I don’t know, the whole thing wont work

Try this

ContextActionService:BindAction(handleAction, true, Enum.KeyCode.E)

His script isnt the problem > I literally threw it in studio to test it. Its something hes doing personally.

You need to show a picture of your explorer

Alright I hope he solves it i’ll try with him too.

RobloxStudioBeta_XIfkIygr27

Show the properties of the problem script > if theres nothing wrong the only logical reason is another script you have is deleting the script or disabling it. As I said I already threw it into studio and it works perfectly fine. You can probably open a new studio and try that script there just to see if some script you currently have is problematic.

This may be useful?, This is the whole script for this, the start of the building system. and i have restarted studio a few times already

local StarterGui = game:GetService("StarterGui")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local mouse = Player:GetMouse()

local Part = Instance.new("Part")
Part.Parent = workspace
Part.Color = Color3.new(0.266667, 0.266667, 0.266667)
Part.Transparency = .5
Part.CanCollide = false
Part.Size = Vector3.new(1,6,4)
Part.Anchored = true


mouse.Move:Connect(function()
	Part.Position = mouse.Hit.Position + Vector3.new(0, Part.Size.Y / 2,0)
end)

while mouse.Idle do
	Part.Transparency = .5
	wait(.1)
	Part.Transparency = .4
	wait(.1)
	Part.Transparency = .5
	wait(.1)
	Part.Transparency = .6
	wait(.1)
	Part.Transparency = .7
	wait(.1)
	Part.Transparency = .6
	wait(.1)
end

print("PO")

function handleAction(actionName, inputState, inputObject)
	if actionName == "PlaceObject" and inputState == Enum.UserInputState.Begin then
		print("Test")
	end
end

ContextActionService:BindAction("PlaceObject", handleAction, true, Enum.KeyCode.E)

Well yeah now it makes sense to why its not working > you have a loop stopping the script from progressing.

Try throwing that loop in a spawn function looks like this

spawn(function()
     while mouse.Idle do
	     Part.Transparency = .5
	     wait(.1)
	     Part.Transparency = .4
	     wait(.1)
	     Part.Transparency = .5
	     wait(.1)
	     Part.Transparency = .6
	     wait(.1)
	     Part.Transparency = .7
	     wait(.1)
	     Part.Transparency = .6
	     wait(.1)
     end
end)

Spawn functions allow you to run a block of code without halting the rest of the script. So its pretty useful for loops such as this.

3 Likes

I never knew that while loops stop the script.

All types of loops will stop the code below it from running since the script is repeatedly running the loop. Also, if the loop ends then the code below it will start to run. Another way you can fix this is by just moving the loop script to the bottom of the script.

Sorry if this is off topic, but why is your studio explorer tab different?