How to convert userinput function into a local function

how would i convert this part of script into a function like “function example()”

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.E and not cooldown then
		shieldActivatedEvent:FireServer()
		local shieldGui = player:FindFirstChild("PlayerGui"):FindFirstChild("ShieldGui")
		if shieldGui then
			local shieldImage = shieldGui:FindFirstChild("ImageLabel")
			if shieldImage then
				shieldImage.Visible = true
			end
		end
		cooldown = true
		task.wait(50)
		cooldown = false
	end
end)
1 Like

Just make the function for the input,

local function Input(Input, Processed)
if Processed then return end
	if input.KeyCode == Enum.KeyCode.E and not cooldown then
		shieldActivatedEvent:FireServer()
		local shieldGui = player:FindFirstChild("PlayerGui"):FindFirstChild("ShieldGui")
		if shieldGui then
			local shieldImage = shieldGui:FindFirstChild("ImageLabel")
			if shieldImage then
				shieldImage.Visible = true
			end
		end
		cooldown = true
		task.wait(50)
		cooldown = false
	end
end

UserInputService.InputBegan:Connect(Input)
2 Likes

The code you provided works, but if you want it to be consistent with some format, you can do it like this:

local function EXAMPLE_NAME(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.E and not cooldown then
		shieldActivatedEvent:FireServer()
		local shieldGui = player:FindFirstChild("PlayerGui"):FindFirstChild("ShieldGui")
		if shieldGui then
			local shieldImage = shieldGui:FindFirstChild("ImageLabel")
			if shieldImage then
				shieldImage.Visible = true
			end
		end
		cooldown = true
		task.wait(50)
		cooldown = false
	end
end)

UserInputService.InputBegan:Connect(EXAMPLE_NAME)

If I remember correctly, the parameters may not pass so you have to do this

UserInputService.InputBegan:Connect(function(...)
    EXAMPLE_NAME(...)
end)

1 Like

i tried both but if i use those it wont even work upon pressing E

1 Like

Are you using a localscript? The normal way I handle keybind abilities in my game is this way.

1 Like

its a local script in starterplayerscripts

1 Like

Is there any input at all or are you loading the character at any point in the game?

1 Like

yes i am should i send full orginial script

1 Like

Yes that would be helpful if you sent the full code

1 Like
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CAS = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local shieldActivatedEvent = ReplicatedStorage:WaitForChild("ShieldActivated")
local cooldown = false

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.E and not cooldown then
		shieldActivatedEvent:FireServer()
		local shieldGui = player:FindFirstChild("PlayerGui"):FindFirstChild("ShieldGui")
		if shieldGui then
			local shieldImage = shieldGui:FindFirstChild("ImageLabel")
			if shieldImage then
				shieldImage.Visible = true
			end
		end
		cooldown = true
		task.wait(50)
		cooldown = false
	end
end)

player.CharacterAdded:Connect(function()
    cooldown = false
end)

shieldActivatedEvent.OnClientEvent:Connect(function()
	local shieldGui = player:FindFirstChild("PlayerGui"):FindFirstChild("ShieldGui")
	if shieldGui then
		local shieldImage = shieldGui:FindFirstChild("ImageLabel")
		if shieldImage then
			shieldImage.Visible = false
		end
	end
end)

CAS:BindAction("ShieldAction", ,true, Enum.KeyCode.E)
CAS:SetTitle("ShieldAction", "Shield")
CAS:SetPosition("ShieldAction", UDim2.new{0.908, 0},{0.6, 0})
1 Like

also now if i press E it wont work

Remove this line

CAS:BindAction("ShieldAction", ,true, Enum.KeyCode.E)

or bind a real function

1 Like

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CAS = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local shieldActivatedEvent = ReplicatedStorage:WaitForChild("ShieldActivated")
local cooldown = false

local function Input(Input, Gpe)
	if Gpe then return end
	if Input.KeyCode == Enum.KeyCode.E and not cooldown then
		shieldActivatedEvent:FireServer()
		local shieldGui = player:FindFirstChild("PlayerGui"):FindFirstChild("ShieldGui")
		if shieldGui then
			local shieldImage = shieldGui:FindFirstChild("ImageLabel")
			if shieldImage then
				shieldImage.Visible = true
			end
		end
		cooldown = true
		task.wait(50)
		cooldown = false
	end
end

player.CharacterAdded:Connect(function()
	cooldown = false
	--
	pcall(function()
		UserInputService.InputBegan:Connect(function(...)
			Input(...)
		end)
	end)
end)

shieldActivatedEvent.OnClientEvent:Connect(function()
	local shieldGui = player:FindFirstChild("PlayerGui"):FindFirstChild("ShieldGui")
	if shieldGui then
		local shieldImage = shieldGui:FindFirstChild("ImageLabel")
		if shieldImage then
			shieldImage.Visible = false
		end
	end
end)

CAS:BindAction("ShieldAction",true, Enum.KeyCode.E)
CAS:SetTitle("ShieldAction", "Shield")
CAS:SetPosition("ShieldAction", UDim2.new{0.908, 0},{0.6, 0})
1 Like

thats the whole point of this post, i want to convert the input into a function

1 Like
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CAS = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")

local player = Players.LocalPlayer
local shieldActivatedEvent = ReplicatedStorage:WaitForChild("ShieldActivated")
local cooldown = false

local function inputfunc()
if not cooldown then
shieldActivatedEvent:FireServer()

		local shieldGui = player:FindFirstChild("PlayerGui"):FindFirstChild("ShieldGui")
		if shieldGui then
			local shieldImage = shieldGui:FindFirstChild("ImageLabel")
			if shieldImage then
				shieldImage.Visible = true
			end
		end
		cooldown = true
		task.wait(50)
		cooldown = false
end
end

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.E  then
		 inputfunc()
	end
end)

player.CharacterAdded:Connect(function()
    cooldown = false
end)

shieldActivatedEvent.OnClientEvent:Connect(function()
	local shieldGui = player:FindFirstChild("PlayerGui"):FindFirstChild("ShieldGui")
	if shieldGui then
		local shieldImage = shieldGui:FindFirstChild("ImageLabel")
		if shieldImage then
			shieldImage.Visible = false
		end
	end
end)

CAS:BindAction("ShieldAction", inputfunc ,true, Enum.KeyCode.E)
CAS:SetTitle("ShieldAction", "Shield")
CAS:SetPosition("ShieldAction", UDim2.new{0.908, 0},{0.6, 0})
1 Like

error is: 22:35:56.799 Players.NoobInAFishTank.PlayerScripts.ShieldActivationClient:12: attempt to index nil with ‘KeyCode’ - Client - ShieldActivationClient:12

1 Like

tsym bro this works with both E, and a button appears that when presses it happens

1 Like

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