Hi, I am trying to create a system similar to the knockback + ragdoll effect when slapping, like in Slap Battles.
The issue is that after slapping the player, the character flings upwards when getting up from ragdoll. I tried solutions from the dev forum, but they did not work.
Ragdoll Module:
local Ragdoll = {}
function Ragdoll.ragdoll(character, knockbackImpulse)
local humanoid = character:FindFirstChild("Humanoid")
local root = character:FindFirstChild("HumanoidRootPart")
if not humanoid or not root then return end
humanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
humanoid.PlatformStand = true
humanoid.RequiresNeck = false
for _, v in ipairs(character:GetDescendants()) do
if v:IsA("Motor6D") then
local att0 = Instance.new("Attachment")
local att1 = Instance.new("Attachment")
att0.CFrame, att1.CFrame = v.C0, v.C1
att0.Parent, att1.Parent = v.Part0, v.Part1
local constraint = Instance.new("BallSocketConstraint")
constraint.Attachment0, constraint.Attachment1 = att0, att1
constraint.LimitsEnabled = true
constraint.TwistLimitsEnabled = true
constraint.UpperAngle = 15
constraint.TwistUpperAngle = 15
constraint.MaxFrictionTorque = 30
constraint.Parent = v.Parent
v.Enabled = false
end
end
if knockbackImpulse then
root:SetNetworkOwner(nil)
root.Anchored = false
root:ApplyImpulse(knockbackImpulse)
end
end
function Ragdoll.unragdoll(character)
local humanoid = character:FindFirstChild("Humanoid")
local root = character:FindFirstChild("HumanoidRootPart")
if not humanoid or not root then return end
root:SetNetworkOwner(nil)
root.Anchored = true
root.AssemblyLinearVelocity = Vector3.zero
root.AssemblyAngularVelocity = Vector3.zero
humanoid.PlatformStand = true
for _, obj in ipairs(character:GetDescendants()) do
if obj:IsA("BallSocketConstraint") then
obj:Destroy()
elseif obj:IsA("Motor6D") then
obj.Enabled = true
end
end
local attachment = root:FindFirstChildWhichIsA("Attachment")
if not attachment then
attachment = Instance.new("Attachment")
attachment.Parent = root
end
local alignPos = Instance.new("AlignPosition")
alignPos.Attachment0 = attachment
alignPos.MaxForce = 1e5
alignPos.Responsiveness = 50
alignPos.Mode = Enum.PositionAlignmentMode.OneAttachment
alignPos.Parent = root
local alignOri = Instance.new("AlignOrientation")
alignOri.Attachment0 = attachment
alignOri.MaxTorque = 1e5
alignOri.Responsiveness = 50
alignOri.Mode = Enum.OrientationAlignmentMode.OneAttachment
alignOri.Parent = root
task.wait(0.1)
alignPos:Destroy()
alignOri:Destroy()
root.Anchored = false
humanoid.PlatformStand = false
humanoid.RequiresNeck = true
humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
local animator = humanoid:FindFirstChildOfClass("Animator")
if animator then
humanoid:Move(Vector3.new(), true)
end
local player = game.Players:GetPlayerFromCharacter(character)
if player then
root:SetNetworkOwner(player)
end
end
return Ragdoll
Damange script in the slap tool
local tool = script.Parent
local re = tool:WaitForChild("RemoteEvent")
local hitbox = tool:WaitForChild("Hitbox")
local Ragdoll = require(game.ReplicatedStorage:WaitForChild("Ragdoll Module"))
local damage = 20
local hitboxActive = false
local hitTargets = {}
local hitCooldown = 0.5
local snd = nil
-- Apply knockback
local function assignKnockback(targetHRP, dir)
local mass = targetHRP.AssemblyMass
local horizontalForce = 200 * mass
local verticalForce = 300 * mass
local impulse = (dir * horizontalForce) + Vector3.new(0, verticalForce, 0)
targetHRP:ApplyImpulse(impulse)
local angularImpulse = Vector3.new(0, 300 * mass, 0)
targetHRP:ApplyAngularImpulse(angularImpulse)
end
local function onHitboxTouched(hit)
if not hitboxActive then return end
local character = hit.Parent
local humanoid = character and character:FindFirstChild("Humanoid")
local targetHRP = character and character:FindFirstChild("HumanoidRootPart")
if not (humanoid and targetHRP) then return end
if character == tool.Parent then return end
local targetId = character.Name
if hitTargets[targetId] and tick() - hitTargets[targetId] < hitCooldown then
return
end
snd = tool:WaitForChild("Slap")
snd:Play()
hitTargets[targetId] = tick()
local dir = (targetHRP.Position - hitbox.Position).Unit
Ragdoll.ragdoll(character)
task.delay(0.05, function()
if targetHRP and targetHRP.Parent then
targetHRP.Anchored = false
local success, err = pcall(function()
targetHRP:SetNetworkOwner(nil)
end)
if not success then
warn("NetworkOwner set failed:", err)
end
assignKnockback(targetHRP, dir)
task.delay(1, function()
local player = game.Players:GetPlayerFromCharacter(character)
if player and targetHRP and targetHRP.Parent then
pcall(function()
targetHRP:SetNetworkOwner(player)
end)
end
end)
end
end)
task.delay(1.5, function()
if character and character.Parent then
Ragdoll.unragdoll(character)
end
end)
end
tool.Equipped:Connect(function()
hitbox.CanCollide = false
hitbox.Touched:Connect(onHitboxTouched)
end)
re.OnServerEvent:Connect(function(player)
if player.Character and player.Character:FindFirstChild(tool.Name) then
hitboxActive = true
print("Hitbox activated for", player.Name)
task.wait(0.3)
hitboxActive = false
print("Hitbox deactivated for", player.Name)
hitTargets = {}
end
end)
Sorry for the poor video quality.