Help with understanding Key detect script

So i’m doing a tutorial on creating a fireball upon pressing Q. But there are somethings i dont understand. Greatly appreciated if any of you can answer them

local UserInputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()

local Debounce = false

UserInputService.InputBegan:Connect(function(Input, IsTyping)
	if not IsTyping then 
		if not Debounce then
			
			
			Debounce = true
			
			if Input.KeyCode == Enum.KeyCode.Q then 
				game.ReplicatedStorage.FireBall:FireServer()
				
				
			end
			
			wait(0.1)
			Debounce = false
		end
	end
end)

What is local Debounce = false?
I thought when doing local “insert a name” = “thing” its to let the script know that when you write that name it means the thing.
But we’re not really writing anything just false so I’m wondering of Debounce already a thing? and how that works cuz I dont really get it. what is if not debounce? like whats it doing and why.

and the If not IsTyping part, whats that supposed to mean? i know we wrote it in the Function but idk? is it a built in function? I’ve heard of that but i dont know what they are. I know that we’re telling it that if they are typing not to run the script but how? I’m guessing its a Built in function or something like that, if so how do i look at all of these built in functions so i can know about them.
Ty for your responses in advance

2 Likes

Heres an example of debounce

local db = false

-- Check if debounce is true if so then return
if db then return end

-- make it true
db = true
-- wait a bit so the player doesnt spam
wait(.3)
-- change debounce back to false so he can shoot again
db = false

and an if blahblah then return end would mean if blahblah == false then return end and not would go with true
istyping checks if the player is typing in the chat or not.

Hope this helped!

2 Likes

Hmm I think i get it.

So IsTyping is a built in function right? Where would i go to see all the built in functions, cuz I have no idea what they are lol.

Also I think i get what we we’re doing with Db/ Debounce but why? Also does it need to be called debounce or is that just what ppl like to use.
Update: I think i suddenly got it tell me if im right. With If db then return end, we’re saying if its false then go back to the start, anyways after that it becomes true, wait 0.3 seconds then it turns false again and since its false it goes back to the start and repeats?
So is this just a way to make something loop, I might be entirely wrong sorry if i’m being bothersome I just want to make sure I understand.

1 Like

Here is all of the built in functions, Yeah basically that

1 Like
local CanShootGun = true;--<Debounce

local function OnShootRequest()
    if(CanShootGun)then
        --<Any thing here will run once "CanShootGun" == true;
CanShootGun = false; --<Now We Make "CanShootGun" == false, so any other OnShootRequest() Won't Reach Here / Won't Work

    wait(5)--<Wait 5 Seconds
    CanShootGun = true --<After 5 Seconds, You Can Now Call A OnShootRequest(), And It'll run the block of code again

    end;
else--<We Won't Get To Here Unless "CanShootGun" == false and we make a OnShootRequest()
    warn("Please Wait Before Trying To Shoot Again");
end;
2 Likes

IsTyping is an argument of ‘InputBegan.’

Dont be fooled though, ‘IsTyping’ can be named anything. It is an argument that tells you whether or not the typing that is being done is a game event. (ie, when typing in a roblox chat.)
‘if not IsTyping’’ – this is literally pseudocode (written out in a very readable manner)
This can also be read as 'if IsTyping == false then"

Debounces in particular prevents code/events from triggering more than once.

Lets take a simple lava brick script.

part.Touched:connect(function(hit) -- Touched event on 'part'
if hit.Parent:FindFirstChild("Humanoid")  then -- if a humanoid is found
hit.Parent.Humanoid:TakeDamage(5) 
end)

-- Now you think that if you touch this lava itll only damage you by 5? WRONG. The touched function will fire the the entire block of code multiple times.
-- This is where a debounce comes in.


local debounce = false
part.Touched:connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid")  then -- if a humanoid is found
if (debounce == false) then -- If debounce is false, then it can proceed to the next line.
debounce = true -- We set it to true so it cant proceed to the next line unless its false.
hit.Parent.Humanoid:TakeDamage(5)  -- takes 5 damage
wait(1) -- Cooldown
debounce = false -- Set to false so it can run again
end)

— By adding the debounce, we prevent the block of code from firing multiple times and added a wait time before setting the debounce false again, which ultimately allows that code of block to run as soon as the Touched event is triggered.

thx for listening to my ted tak

2 Likes

ooo thanks for the example! I understand it allot more now :slight_smile:

ye bro i tried to explain it in its most basic form. np, thisll be a staple for future scripting

1 Like

Thanks for explaining that IsTyping is an argument of Inputbegan i was very confused lol. Is there a way to look at every argument? I just feel like if i were to try and make this script on my own I’d be at a loss because i wouldnt know where to find this information, like the existance of IsTyping lol.

shoot thought that reply was for me LOL.

Dont worry <3 u 2 LOL

CharacterLimit)______

1 Like

You can see the arguments of events at developer.roblox

1 Like

I dont see IfTyping in there? maybe im lookiong in the wrong place, sorry im not the brightest. Plz correct me if im missing something

1 Like

IfTyping in that case would be its second argument, which is orginally called ‘gameprocessedevent.’
If set to true, your keybinds (in your case, Q for fireball if i rmbr) will fire even though youre typing in the game chat.
If set to false, it wont fire

1 Like

Hmm so GameProcessedEvent checks if you’re in the chat? or IfTyping does, and does it have to be called IfTyping?

I get ur saying that IfTyping goes in its place, but how would I know for example that I’m suppose to put IfTyping in its place for this scenerio, is there a thing that tells me about stuff like “IsTyping” correct me if I misunderstand

1 Like

No sir, the argument can be anything you want, people just give it names like ‘IsTyping’ to better understand what gameprocessedevent does.
Ok so if you replace the line from:
‘if not IsTyping’
into
‘if IsTyping’

If you do that, the fireball will run when you press Q while typing.
If you leave it at if not, then it wont fire your fireball

2 Likes

You can call the statement anything as long as its the second argument in the function for example i can call it ‘playertyping’ or ‘istyping’ or ‘typinginthechat’, etc

Check out @Infocus’s answer too

2 Likes