Hello there, I have a question about what I am doing wrong.
So AmouseClick() is a function that compares Mouse.Target
and the humanoid's torso's distance between eachother. It says that if the distance is less than the range it would cause damage to Mouse.Target.Parent.Humanoid
My problem however is that I add debounce in a way different from yours, Instead I disconnect the event nested in the function as highlighted with a comment.
No errors, does not disconnect at all, (tried removing the reconnecting part and still doesn't disconnect) and just let's the player spam their mouse.
The debounce is running once. I'm not fully sure of how to debounce since I'm not sure if events still do listen while a wait()
is yielding the script. Just think of it has people walking into a door closed, won't they still walk in once the door is open?
Note: variable mouseEvent
is stated before this function and is assigned an event after this function.
local function AmouseClick() if Settings.Class == nil then return nil end print("Mouse Clicked") if not debounce then if player.Team == game.Teams.Zombies then if mouse.Target.Parent:FindFirstChild("Player") then local targetPlayer = game.Players:GetPlayerFromCharacter(mouse.Target.Parent) if targetPlayer.Team == game.Teams.Survivors then local distance = (character.HumanoidRootPart.Position - mouse.Target.Position).Magnitude if distance < Settings.Range then local humanoid = mouse.Target.Parent.Humanoid humanoid:TakeDamage(Settings.Damage) end end end end debounce = true else print("Disconnecting mouse event") --This is the problem mouseEvent:Disconnect() wait(Settings.AttackTime) mouseEvent = mouse.Button1Down:Connect(AmouseClick) end end mouse.Button1Down:Connect(AmouseClick)
Thank you for viewing this!
Your mouseEvent isn't assigned anything when the script runs. On line 29 you're simply stating that when the mouse is clicked it'll trigger. You need to actually assign mouseEvent on line 29 the same way you assigned it on 25 like:
mouseEvent = mouse.Button1Down:Connect(AmouseClick)
Also, the script will run regardless of if script is held up by a wait. I'm not sure on the specifics but let's put it like this, if you tell a script to print "Hello" after waiting 3 seconds and you rapidly clicked about 10 times. Then 3 seconds later you'd see "Hello" print 10 times.
Assuming I understand exactly what your script is intending to do, this should work:
local function AmouseClick() if Settings.Class == nil then return nil end print("Mouse Clicked") if player.Team == game.Teams.Zombies then if mouse.Target.Parent:FindFirstChild("Player") then local targetPlayer = game.Players:GetPlayerFromCharacter(mouse.Target.Parent) if targetPlayer.Team == game.Teams.Survivors then local distance = (character.HumanoidRootPart.Position - mouse.Target.Position).Magnitude if distance < Settings.Range then local humanoid = mouse.Target.Parent.Humanoid humanoid:TakeDamage(Settings.Damage) end end end end print("Disconnecting mouse event") mouseEvent:Disconnect() wait(Settings.AttackTime) mouseEvent = mouse.Button1Down:Connect(AmouseClick) end mouseEvent = mouse.Button1Down:Connect(AmouseClick)
You have to define Mouse Event before you do anything with it. That's like reading a book backwards.
print("Disconnecting mouse event") --This is the problem mouseEvent = mouse.Button1Down:Connect(AmouseClick) wait(Settings.AttackTime) mouseEvent:Disconnect()
See if this works
local function AmouseClick() if Settings.Class == nil then return nil end print("Mouse Clicked") if not debounce then if player.Team == game.Teams.Zombies then if mouse.Target.Parent:FindFirstChild("Player") then local targetPlayer = game.Players:GetPlayerFromCharacter(mouse.Target.Parent) if targetPlayer.Team == game.Teams.Survivors then local distance = (character.HumanoidRootPart.Position - mouse.Target.Position).Magnitude if distance < Settings.Range then local humanoid = mouse.Target.Parent.Humanoid humanoid:TakeDamage(Settings.Damage) end end end end debounce = true else print("Disconnecting mouse event") --This is the problem mouseEvent:Disconnect() wait(Settings.AttackTime) mouseEvent = mouse.Button1Down:Connect(AmouseClick) debounce = false --//We have to make sure that the debounce gets reset or the function won't work the way it was intended. end end mouseEvent = mouse.Button1Down:Connect(AmouseClick)