Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I bind more than one action to a single keycode using context action service?

Ok mod apparently I wasn't specific enough. What I'm trying to do is have the player do multiple actions (i.e. entering a vehicle and opening doors) using one key on the keyboard. Having a different for each action is pretty cumbersome so that's why I decided to do this. I tried doing what another post suggested which was this:

function door(state, input)
	if (state == Enum.UserInputState.Begin) then
   		  --script to open door
	end
end

function vehicle(state, input)
	if (state == Enum.UserInputState.Begin) then
		--enter vehicle
	end
end

function EStuff()
    door()
    vehicle()
end

game:GetService("ContextActionService"):BindAction("EStuff", EStuff, false, Enum.KeyCode.E)

But it didn't work. Any help is appreciated. This is just an example script btw, everything works when I bind a key to one action.

4 comments

0

facts lol

0

wut r facts you be comfusle me

0

also is there any erros press f9 if u cant know how see erros

0

hmmm

1 answer

✓ Accepted answer

Perhaps you could check to see whether the user is in range of a door or a car and then act accordingly.

The idea would be to detect the key input and then cast a ray away from the player. The ray would be used to check what is near to the player so that they may act accordingly.

I can't promise the code below would work but you should get the right idea.

local Player = game:GetService("Players").LocalPlayer
local playerPart = Player:FindFirstChild("HumanoidRootPart")
local Length = 15

local function checkPosition()
	local Ray = Ray.new(playerPart.Position, playerPart.LookVector*Length)
	local Door, HitPosition = workspace:FindPartOnRay(Ray, AI)
	if Door then
	    -- Do door action
	else
	    -- Do car action
	end
end)

local function door(state, input)
    if (state == Enum.UserInputState.Begin) then
	checkPosition()
    end
end

game:GetService("ContextActionService"):BindAction("EStuff", EStuff, false, Enum.KeyCode.E)

3 comments

0

I already have it so that the door and car is detected by using collection service tags. :c Thanks for answering tho

0

The same concept would apply here. Search for what is nearby and act accordingly

0

hmmm i'll try it

This discussion is preserved as it was. New answers, comments, and votes are closed.