Task.wait() not working

i have a local script that fires the server and the server will get an animation dependent on a numbervalue in the players character, but you can spam click it and the task.waits wont stop it and i dont know what to do

--this is the client firing to server
local plr	: Player			= game.Players.LocalPlayer
local char	: Character			= plr.Character or plr.CharacterAdded:Wait()
local hum	: Humanoid			= char:WaitForChild("Humanoid")
local hrp 	: BasePart			= char:WaitForChild("HumanoidRootPart")
local uis	:UserInputService	= game:GetService("UserInputService")
local SCHBE : RemoteEvent		= game.ReplicatedStorage.Events:WaitForChild("ServerConnectionM1Event")
local punchFolder				= game.ReplicatedStorage:WaitForChild("Animations"):WaitForChild("PLACEHOLDERM1S")

local function HandleInput(input : InputObject,gpe)
if gpe then
		return 
	end
if input then
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
			SCHBE:FireServer(char,hum,hrp, punchFolder)
		end
	end
end		


uis.InputBegan:Connect(HandleInput)
--this is server
local m1Event : RemoteEvent = game.ReplicatedStorage.Events:WaitForChild("ServerConnectionM1Event")
local lasttime = 0

local function GetM1Num(plr : Player,char : Character ,hum : Humanoid,hrp : BasePart, AnimFolder : Folder)
	local currentp = tick()
	if currentp - lasttime >3 then
		char.M1Val.Value = 0
	end
	local M1Service			= require(script.M1Service)
	local M1Val = char["M1Val"]
	if M1Val.Value == 1 then
		M1Val.Value += 1
		M1Service.HandleM1s(plr,char,hum,hrp,AnimFolder,1)
		task.wait(.7)
	elseif M1Val.Value == 2 then
		M1Val.Value += 1
		M1Service.HandleM1s(plr,char,hum,hrp,AnimFolder,2)
		task.wait(.7)
	elseif M1Val.Value == 3 then
		M1Val.Value += 1
		M1Service.HandleM1s(plr,char,hum,hrp,AnimFolder,3)
		task.wait(5)
	elseif M1Val.Value == 0 then
		print("Tried")
		M1Val.Value += 1
		M1Service.HandleM1s(plr,char,hum,hrp,AnimFolder,0)
		task.wait(.7)
	end
	lasttime = tick()
end

m1Event.OnServerEvent:Connect(GetM1Num)

You can try to implement a debounce

local debounce = false

local function GetM1Num(...)
   if (not debounce) then
      debounce = true
      -- Your code with your if statements and your task.wait
      debounce = false -- After the delay put it back to false
   end
end

1 Like

When the m1Event event is fired, it ignores the task.wait(duration : number) function because although it is running the same GetM1Num function, they are completely separate from each other.

To counter this problem, you must create a variable that detects if the GetM1Num function executed already. In programming terms, this is called Debouncing.

local debounce = false

local function GetM1Num(plr : Player,char : Character ,hum : Humanoid,hrp : BasePart, AnimFolder : Folder)
	if debounce then return end --stop the function if debounce is true
	debounce = true --makes future functions not run because of the debounce check
	--code here
	debounce = false --makes future functions run
end
1 Like

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