I have a script that handles multiple actions based on button input, each coroutine is in a dicitonary with it’s corresponding KeyCode
[Enum.KeyCode.Q] = coroutine.create(function(Character)
local HRP = Character.HumanoidRootPart
local selfHumanoid = Character.Humanoid
local animation = game.ReplicatedStorage.Animations.Animation
local raycastparams = RaycastParams.new()
raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
raycastparams.FilterDescendantsInstances = {HRP.Parent}
while true do
local result = workspace:Raycast(HRP.Position , HRP.CFrame.LookVector * 5, raycastparams)
local AnimTrack = selfHumanoid.Animator:LoadAnimation(animation)
AnimTrack:Play()
if result then
local humanoid = result.Instance:FindFirstAncestorWhichIsA("Model"):FindFirstChildWhichIsA("Humanoid")
print(humanoid , result.Instance , result.Instance.Parent)
if humanoid then
humanoid:TakeDamage(10)
end
end
coroutine.yield()
wait(2)
coroutine.yield()
end
end)
When I press q, the script checks wether the coroutine is suspended or not, and if it is, resumes it, the problem is that i can trigger it multiple times a second when it should wait 2 seconds the second time i trigger it.
Yeah it is there already, the problem is that it always acts as if the coroutine were suspended even if it’s clearly not, so i am starting to wonder if a new instance of the coroutine is made every time every time it’s resumed by the function.
local onQPressed = coroutine.create(function(Character)
local HRP = Character.HumanoidRootPart
local selfHumanoid = Character.Humanoid
local animation = game.ReplicatedStorage.Animations.Animation
local raycastparams = RaycastParams.new()
raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
raycastparams.FilterDescendantsInstances = {HRP.Parent}
while true do
local result = workspace:Raycast(HRP.Position , HRP.CFrame.LookVector * 5, raycastparams)
local AnimTrack = selfHumanoid.Animator:LoadAnimation(animation)
AnimTrack:Play()
if result then
local humanoid = result.Instance:FindFirstAncestorWhichIsA("Model"):FindFirstChildWhichIsA("Humanoid")
print(humanoid , result.Instance , result.Instance.Parent)
if humanoid then
humanoid:TakeDamage(10)
end
end
coroutine.yield()
wait(2)
coroutine.yield()
end
end)
[Enum.KeyCode.Q] = coroutine.resume(onQPressed)