I’m working on a hitbox, but theres somthing that i cant quite figure out how to do.
The script ragdolls the user. I want it to unragdoll after 2 seconds, but I also want it to reset the timer and replace it with a new timer if another move hits (example: user hits first move that ragdolls for 2 seconds hits, and then someone else or the user uses another move that ragdolls for 3 and hits the player that is already ragdolled. The player then has to wait 3 seconds before unragdolling instead of 2).
Also note that this destroys from another script 0.1 seconds after spawning in, so creating a module script that does all the timed ragdoll stuff would be best for this.
script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Ragdoll = require(ReplicatedStorage.RagdollModule)
local TimedRagdoll = require(game.ServerScriptService:WaitForChild("RagdollManager"))
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local hitboxPart = script.Parent
local DAMAGE = script.Parent.Damage.Value
local KNOCKBACK_FORCE = 50
local hitPlayers = {}
local senderName = script.Parent.Sender.Value
for _, part in pairs(workspace:GetPartsInPart(hitboxPart)) do
local character = part:FindFirstAncestorOfClass("Model")
local humanoid = character and character:FindFirstChild("Humanoid")
local root = character and character:FindFirstChild("HumanoidRootPart")
if humanoid and root and part.Name ~= senderName then
if not table.find(hitPlayers, character.Name) then
table.insert(hitPlayers, character.Name)
print("Hit:", character.Name)
humanoid:TakeDamage(DAMAGE)
local direction = (root.Position - hitboxPart.Position).Unit
root.Velocity = direction * KNOCKBACK_FORCE
Ragdoll.RagdollCharacter(character)
end
end
end
Hope this logic could help ^^
(note: I’m using Luau’s typed variables and I’m writing this in a browser so there may be some errors)
Make a table with charater model as a key and value as a thread
eg. local timerThread: {[Model]: thread} = {}
When a player hit, Check if timerThread[Player's Character] exists or not, if no (returned nil) then ragdoll the player and create a new timer that stops ragdoll after a certain time then remove(timer) itself from the timerThread table. After that, store that new timer into the timerThread table.
if there already exists a timer thread in the table (timerThread[Player's Character] doesn’t return nil), cancel that timer and then creates a new one.
Assuming RagdollChar(character: Model) ragdolls the player and StopRagdoll(character: Model) stop player from ragdoll state
eg.
local timerThread: {[Model]: thread} = {}
local function createTimer(character: Model, sec: number): thread
return task.delay(
sec ,
function()
StopRagdoll( [[Player's Character]] )
timerThread[ [[Player's Character]] ] = nil
end
)
end
-- Somewhere in the touched event
--[[Assuming]] local char: Model = [[Player's Character]]
--[[Assuming]] local secs: number = [[Unragdoll after ... seconds]]
if not timerThread[char] then
RagdollChar(char)
timerThread[char] = createTimer(char, secs)
else
task.cancel(timerThread[char])
timerThread[char] = createTimer(char, secs)
end
I already have a ragdoll module, I just want a module script that, when a function is called from a different script, will add a timer that the script that called the function will input. And if the same function is then called again from a different script, it will not only reset the timer, but replace it with the new one. I will do all the ragdoll stuff, I just need help writing the timer script.
I found an example script and tweaked it, and now it works perfectly.
finished script:
local StunManager = {}
local activeTimers = {}
local Ragdoll = require(game.ReplicatedStorage.RagdollModule)
function StunManager.StartEffectTimer(character: Model, duration: number)
if not character or not character:IsA("Model") then
warn("Invalid character")
return
end
if activeTimers[character] then
task.cancel(activeTimers[character])
end
Ragdoll.RagdollCharacter(character)
wait()
Ragdoll.RagdollCharacter(character)
activeTimers[character] = task.delay(duration, function()
activeTimers[character] = nil
Ragdoll.UnragdollCharacter(character)
end)
end
return StunManager