Hey, thanks for reading my post. I’m trying to make a small box (essentially just a hitbox) attached to the player’s right fist detect if it is touching a humanoid during a punch/slap animation. (I understand that right now it detects anything, not just humanoids, but that’s not the most important part for me right now).
The animation plays and cues the RemoteEvent, which this script checks for. Everything works as intended, except for when I do the animation but nothing is hit, the function keeps checking if it’s being touched, and can give me a result for something touching the box after the animation had already finished for multiple seconds.
I’m unsure if I could use return
here, as I only want it to stop checking once the animation is completed (~0.2s). My idea was to have the script count for ~.25 seconds and then end the function if it doesn’t touch something within that timeframe, but I don’t know how to do that in a script that would also carry out the action if it is touching something during those .25 seconds.
Here’s my current script:
local Remote = game:GetService("ReplicatedStorage"):WaitForChild("Slap")
Remote.OnServerEvent:Connect(function(Player)
local SlapBox = Player.Character:WaitForChild("SlapBox")
game:GetService("ReplicatedStorage"):WaitForChild("Woosh"):Play() -- Sound effect, unimportant rn.
local Slap
Slap = SlapBox.Touched:Connect(function(Hit)
-- Will add knockback or whatever right here.
local HitHumanoid = Hit.Parent:FindFirstChild("Humanoid")
HitHumanoid.Sit = true
print(Hit) -- So I know if it's a hit and where.
game:GetService("ReplicatedStorage"):WaitForChild("Smack"):Play() -- Another sound effect
Slap:Disconnect()
end)
end)
Thank you in advance for your help.