So while i was making a mario-like platformer i decided to add firing a projectile to my game. At first i added the F = a new projectile that heads thru the general direction, then i added a hold F to spray projectiles. I decided to add a cooldown of 0.3 to every bullet fired and after doing the cooldown for the hold F mechanic i decided to TRY adding the same cooldown mechanic to the normal projectile spawning without holding. Homever no matter what i did it would never work. It would either keep spraying bullets even when im not holding F or just not working. But later i came up with a very good solution that somehow dosent work. Also no there are no errors. The local script:
local UserInputService = game:GetService("UserInputService") local KeyPressed = false local CoolDown = false local function CoolDown() CoolDown = true wait(0.3) CoolDown = false end UserInputService.InputBegan:Connect(function(input) if input.KeyCode == Enum.KeyCode.F then if CoolDown == false then game.ReplicatedStorage.ProjectileFire:FireServer() KeyPressed = true end while KeyPressed == true do wait(0.3) game.ReplicatedStorage.ProjectileFire:FireServer() end end end) UserInputService.InputEnded:Connect(function(KeyCode) if KeyCode.KeyCode == Enum.KeyCode.F then KeyPressed = false end end)
The Server script:
game.ReplicatedStorage.ProjectileFire.OnServerEvent:Connect(function(plr) print("pew pew accepted") local Char = game.Workspace:WaitForChild(plr.Name) local ProjectileOrgin = game.ReplicatedStorage.Projectile local ProjectileClone = ProjectileOrgin:Clone() ProjectileClone.Parent = game.Workspace ProjectileClone.Position = Char.HumanoidRootPart.Position end)
Please come up with a solution cus this has absolutely stomped me. :D