Am I doing this the right way?

So basically, I’m working on this soccer system, and I have certain “methods” of kicking the ball for certain tools, for example, for the pass tool, you can do “high pass, low pass, backspin, etc” and the way it works is, if ur holding mousebutton1 and u press the key while charging, it knows to do that specific method of kicking the ball. But since all my other scripts are also checking for mousebutton1, the kick-power “power” gets charged for every other script aswell.

local function onInputBegan(input, isProcessed)
	if isProcessed or not expectingInput then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		isInputActivated = true
		isKeyDown = false
		power = 25
		while isInputActivated do -- While mouse is down
			if UserInputService:IsKeyDown(Enum.KeyCode.Q) then -- If they press the key
				isKeyDown = true
				print("Triggered")
			end
			power = math.min(power + 1, MAX_POWER)
			print(power)
			task.wait(1/60)
		end
	end
end

If you’re confused, here you can see in the image that when I hold my mouse down, it is charging the kicking power for multiple scripts. I’m just wondering if this is the right way to do it. Everything works, I’m just wondering if I should be doing something else to optimize it.
Screenshot 2024-05-05 111307

Tools have a .Equipped and .Unequipped event you can listen to. You can connect a function to both and update a variable inside your code based on said events firing and keep track of which tool is equipped. An example of doing that could look like this:

local tool: Tool =  --Path to your tool
local equipped: boolean = false

--Runs when the player equips the tool
tool.Equipped:Connect(function()
	equipped = true
end)

--Runs when the player unequips the tool
tool.Unequipped:Connect(function()
	equipped = false
end)

You can write a condition inside your onInputBegan() function that makes sure the player is actually holding the tool before charging the kick.

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