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
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.
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.
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;
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
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.
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
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
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
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