How to make an ability charge-able

Hello, I’ve wanted to try and make my fireball ability charge-able, making it deal more damage and create a larger explosion plus giving it more room to aim.

Currently the ability just instantly casts upon pressing the button.
I know I have to use the UserInputService InputBegan and InputEnded, but I’m confused on how I can replicate its effects into the server. Also trying to make this work on mobile and console. :,)

So in summary, I just need a general base on how to do it…

2 Likes

I went ahead and made a simple script that will provide somewhat of a foundation to which you can further make your code off of. The script is easily customizable and already made spam-proof with a debounce. If you have questions, let me know.

Show code

What you need:

image

  • ServerScript
  • 2x RemoteEvent
  • LocalScript

LocalScript in StarterPlayerScripts

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

local player = Players.LocalPlayer --//Defines who the script will be for

local debounce = false

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E and debounce == false then
		debounce = true
		
		ReplicatedStorage.StartCharge:FireServer(player) --//Starts the charge serversided
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E and player:FindFirstChild("Charging") then
		
		ReplicatedStorage.EndCharge:FireServer(player) --//Ends the charge.
		
		task.wait(3)
		debounce = false
		print("Debounce set to ", debounce)
	end
end)

ServerScript

local ReplicatedStorage = game:GetService("ReplicatedStorage") --//Clear a path to the RemoteEvents

ReplicatedStorage.StartCharge.OnServerEvent:Connect(function(player)
	
	local charging = Instance.new("IntValue") --//Create the charge
	charging.Parent = player
	charging.Name = "Charging"
	charging.Value = 0 --//Charge count
	
	while player:FindFirstChild("Charging") do
		player.Charging.Value += 1
		print(player.Charging.Value)
		task.wait(1) --//Charge will increase by 1 every 1 second. Customize how you'd like
	end
end)

ReplicatedStorage.EndCharge.OnServerEvent:Connect(function(player)
	print(player.Name .. " attacked with a charge strength of " .. player.Charging.Value)
--[[
Before the next line it gets destroyed, this player.Charging.Value will be your charge value.
Use this value as a factor to determine the strength/stage of the charge.
]]
	player.Charging:Destroy() --//Delete for proper reset of charge
end)
2 Likes