How do i make a charging system?

ive tested with printing but when i click it without holding it still prints maybe im doing it wrong

It still counts as a charge by just tapping, since you technically start the input and end it; it would just be insignificant. You could prevent the throwing mechanic from happening if the charge was not long enough, after the charge already took place.

1 Like

dang the other guy beat me to it :smiling_face_with_tear:

wellā€¦ hereā€™s basically the same thing anyways lol

local UIS = game:GetService("UserInputService")

local activationKey = Enum.KeyCode.LeftShift -- change to key that you want

local timestamp

local function TestFunction(Data) -- replace with actual function
	-- task.spawn(function() -- this makes it so it doesn't stop the other stuff when you run the function, if you want/need it
	local threshold = 500 -- threshold to actually do something/do a specific thing
	if Data > threshold then
		warn("woah!") -- replace with actual code
	end
	print(Data)
	--)
end

UIS.InputBegan:Connect(function(key, isProcessed)
	if key.KeyCode == activationKey and not isProcessed then 
		timestamp = DateTime.now().UnixTimestampMillis -- basically just rn, in a useable form
		-- additional functions and stuff here --
	end
end)

UIS.InputEnded:Connect(function(key, isProcessed)
	if key.KeyCode == activationKey and not isProcessed then
		local length
		if timestamp then
			length = DateTime.now().UnixTimestampMillis - timestamp
		end
		TestFunction(length) -- replace with actual function
		timestamp = nil
		-- additional functions and stuff here --
	end	
end)

and I switched to DateTime.now() because itā€™s probably better than tick(), basically just like what the other guy did lol

2 Likes

ive researched found out tick was time in second past since 1970 but Datetime.fromUnixTimestampMillis was time in second past since 1400ā€¦ bro i was also shocked

2 Likes

I recommend using the threshold as rankydoodle has if you want to keep a limit on how short the charge can be, but the rest is basically the same either way. Good luck!

2 Likes

Have either of our scripts solved your problem? If so, mark one of them as a solution, as it will help people in the future and close the thread.

2 Likes

i have a rough time undertanding the script

1 Like

Which part are you having trouble with?

1 Like

i kind of understand it now. now i just need to add the limit thing but thats the part im a bit confused in

1 Like

Hi Ingat, I didnā€™t follow the discussion but I decided to show you how I would do this kind of thing.

-- Services
local UserInputService = game:GetService("UserInputService")
local PlayerService = game:GetService("Players")

-- Variables
local Player = PlayerService.LocalPlayer

local ActivationKey = Enum.UserInputType.MouseButton1 -- Change to key that you want

local HoldDuration = 5 -- In seconds
local ChargingValue = 0

local StartingTick = 0

local Holded = false

local BillboardGui = script.Parent
local HoldingBar = BillboardGui:WaitForChild("HoldingBar")
local LoadingBar = HoldingBar:WaitForChild("LoadingBar")

-- Functions
local function ThrowFootball()
	print(ChargingValue)
end

local function NewCharacter(character)
	local head = character:WaitForChild("Head")
	BillboardGui.Adornee = head
end

-- Initialize Character
if Player.Character then
	NewCharacter(Player.Character)
end

-- Connections
Player.CharacterAdded:Connect(NewCharacter)

UserInputService.InputBegan:Connect(function(inputObject)
	if (inputObject.UserInputType == ActivationKey) then
		-- Reset Values
		ChargingValue = 0
		
		LoadingBar.Size = UDim2.new(0, 0, 1, 0)
		
		-- Update Starting Tick
		StartingTick = tick()
		
		-- Enable UI
		BillboardGui.Enabled = true
		
		-- Update Holded Value
		Holded = true
	end
end)

UserInputService.InputEnded:Connect(function(inputObject)
	if (inputObject.UserInputType == ActivationKey) and Holded then
		-- Update Holded Value
		Holded = false
		
		-- Disable UI
		BillboardGui.Enabled = false
		
		-- Call Throw
		ThrowFootball()
	end
end)

-- Initialize
while true do
	if Holded then
		-- Normalized Value
		local alpha = ((tick() - StartingTick) / HoldDuration)
		
		-- Update Charging Value
		ChargingValue = math.clamp(alpha, 0, 1)
		
		-- Update 
		LoadingBar.Size = UDim2.new(ChargingValue, 0, 1, 0)
	end
	
	task.wait()
end


Velocity.rbxl (57,8 Ko)

ChargingValue would therefore be the force to apply. So you could multiply the maximum force with this value to get the right force to apply. Try to ensure that the force to be applied is added to the minimum force to be sure that the result is never 0

local function ThrowFootball()
	local addingForce = (MAX_FORCE * ChargingValue)
	local velocity = (MIN_FORCE + addingForce)
	print(velocity)
end

I hope you find my post a little bit useful. I wish you the best for your upcoming project!

5 Likes

you guys all did amazing thanks. currently im making a football game with the mechanics similarly like blade ball, ill probably put updates on the game im making thank you guys.

1 Like

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