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)
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)
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
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})
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})
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})
error is: 22:35:56.799 Players.NoobInAFishTank.PlayerScripts.ShieldActivationClient:12: attempt to index nil with ‘KeyCode’ - Client - ShieldActivationClient:12