How do i make a charging system?

im making a football game where when you charge/hold click the force gets stronger

And im wondering how to make that

This is the example footage:

4 Likes

You can have a variable called multiplier and increase it by x amount for every y second that it is being held down.

3 Likes

do i have to use while loops or not?

2 Likes

repeat
wait().1
multiplier += .2
until mouse.button1up

something like that

2 Likes

what about user input service?

I made a charging system detecting when mousedown with UIS and a repeat loop, and when mouse button up, it stops charging

2 Likes

like this?

local UIS = game:GetService("UserInputService")
local hold = false

local Multiplier = 5

UIS.InputBegan:Connect(function(input, gameProccesed)
	if gameProccesed then
		return
	end

	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		hold = true
		
		if hold == true then
			repeat wait(.1)
				Multiplier += .2
				print(Multiplier)
			until UIS.InputEnded
		end
	end
end)

UIS.InputEnded:Connect(function(input, gameProccesed)
	if gameProccesed then
		return
	end
	
	hold = false
end)

Have you tried it?

yes ive tested it, but im kind of confused

idk it was in an old game 30cccc

mouse.button1up? is this for a gui

idk i forgot

1 Like

uhhh are there any solution guys?

think I have an idea. User input service > detect key press > if key you want is pressed, make a timestamp like with tick()
User input service > detect key end pressing > if key is no longer pressed and timestamp exists, detect length of time between right then and timestamp (now - timestamp = length), if length is greater than some threshold, do thing, remove timestamp. I’ll probably write real code in a little bit

1 Like

I would probably use DateTime.now(), as to avoid using a while loop and keep it working efficiently. If you are worried about an animation, I would play the animation and stop it when the player unclicks.

It would look something like this:

local UIS = game:GetService("UserInputService")

local isCharging = false -- make sure it only releases after charging
local startTime; -- define when starting charge
local endtime; -- define when ending charge
local timeMultiplier = 1 -- change according to how much you want the time spent charging to affect the release

function throwFootball()
        local timeCharged = endTime - stopTime
        local chargeResult = timeCharged * timeMultiplier
        -- THROW FOOTBALL WITH RESULT
end

UIS.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton2 then
        startTime = DateTime.now().UnixTimestampMillis
        isCharging = true
        -- START ANIMATION
    end
end)

UIS.InputEnded:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton2 and isCharging then
        endTime = DateTime.now().UnixTimestampMillis
        isCharging = false
        -- END ANIMATION
        throwFootball()
    end
end)

1 Like

by the way what is datetime? im curious ok

DateTime is a way to get the exact date and time in Roblox. I used DateTime.now() to get the current date and time. I got the property “.UnixTimestampMillis” of DateTime.now() so that it only gives me the time in milliseconds, which keeps the charging accurate. If I had used only normal seconds, it would have only counted 1 second even if you had charged for 1.99 seconds, making it really inaccurate. I hope this helps, and feel free to ask any additional questions.

2 Likes

ive tested it, it has an error: 13:24:01.206 Players.How4rdy.PlayerScripts.LocalScript:10: attempt to perform arithmetic (sub) on number and nil - Client - LocalScript:10

im guessing its because of stoptime what do i do with stoptime cause theres no variable named stoptime

That is because I made a typo. The top variable should be “endTime” not “endtime”. And replace stopTime with startTime.

1 Like