Killing player and leave dead body lags

trying to make system where it leaves player ragdoll body and flings it
image
made red cube, click on red cube it flings and ragdolls player
if player health is 0 or below it’ll leave the player dead body
however im getting problems with it, every time it kills the player there is noticeable lag
and prints out this message

after player respawns everything is ok

code for button

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local RagdollModule = require(ServerStorage.RagDoll) --ragdolls player

local flingEvent = ReplicatedStorage.FlingCharacter --flings player
local controlEvent = ReplicatedStorage.ControlsEvent --enable/disable player controls
local ragdollState = ReplicatedStorage.RagdollState --enable/disable ragdoll state

local part = script.Parent

local click = part.ClickDetector

local flingForce: number = 500
local damage: number = 50

click.MouseClick:Connect(function(player)
	local char = player.Character
	if not char.Humanoid then return end
	controlEvent:FireClient(player, false) --disable controls
	ragdollState:FireClient(player, true) --enable ragdoll state
	RagdollModule.ragdoll(char)
	flingEvent:FireClient(player, (char.HumanoidRootPart.Position - part.Position).Unit, flingForce) --fling character on client
	char.Humanoid.Health -= damage
	if char.Humanoid.Health == 0 then
		char.HumanoidRootPart:SetNetworkOwner(player) --not sure what this do
	else
		task.wait(2)
		RagdollModule.unragdoll(player.Character)
		ragdollState:FireClient(player, false) --disable ragdoll state
	end
	controlEvent:FireClient(player, true) --enable controls
end)

if you need any code i’ll provide it to you

2 Likes

There’s probably an issue with flingEvent, what code is in this RemoteEvent?

I got a Script similar to your engine you have.

Let me know if you need it @IceCreamPickels

1 Like

can you please send it to me, I’ll take a look at it

1 Like

The fling event works perfectly fine, it’s only on the client since the server can’t apply impulse on the humanoid root part,

the code for fling event is literally

flingEvent.OnClientEvent:Connect(function(direction, force)
	rootPart:ApplyImpulse(direction * force)
end)

it only lags when I kill the character itself

probably the main issue happens in the death script,
player ragdolls on death and puts all player body parts into a new model, so it doesn’t despawn
–here’s the death script

local ServerStorage = game:GetService("ServerStorage")
local ragdoll = require(ServerStorage.RagDoll)

local character = script.Parent
local humanoid = character.Humanoid

humanoid.BreakJointsOnDeath = false

humanoid.Died:Connect(function()
	ragdoll.ragdoll(character)

	local model = Instance.new("Model")
	model.Parent = workspace

	for _, part in pairs(character:GetChildren()) do
		if part.Name == "HumanoidRootPart" then continue end
		part.Parent =  model
	end
end)

–note if thats not enough for you
then here’s the place download
flingragdoll.rbxl (53.8 KB)

After looking around, i suspect its to do with the Control Event. Not sure yet, but still looking

I think i found the problem.

controlEvent:FireClient(player, true) needs to be fired once the player’s character respawns. Instead, its being fired when the current character is dead.

the control event only disable’s player controls so they cant move while ragdoll

local PlayerModule = require(LocalPlayer.PlayerScripts:WaitForChild("PlayerModule"))
local Controls = PlayerModule:GetControls()

local controlEvent = ReplicatedStorage.ControlsEvent

controlEvent.OnClientEvent:Connect(function(enable)
	if enable then
		Controls:Enable()
	else
		Controls:Disable()
	end
end)

well when the character dies, if controlEvent:FireClient(player, true) is sent, then it throws that error and causes lag. You need to check if the character is dead, and if it is, then dont send that event

Control needs to stay off until they player’s character is fully respawned

i just disabled the script, and it does not cause lag
im thinking the lag is probably created by one of the core scripts, but i dont have access to it and nor should i change any of its code

It is. You just need to reenable controls when the player respawns (If they died)

I deleted line 32 under the click detector script and it removed all lag

uh did you set the damage to 100?
because it still lags

also resetting causes it to lag as well

A decided to test it after removing line 20 aswell. The player cant move when they’re ragdolled no matter what. So it looks like changing the control state of the player was un-needed

NVM

you might have to kill yourself multiple times then the lag starts adding up
probably at least 5 times
there is a dramatic drop in fps

you’re probably right on the controls, i probably don’t need it, i only had it incase because at one point the player walking while ragdoll looked kinda silly

You do need to disable control on death. I mispoke. Also heres a video demonstrating

1 Like

Alrighty, hopefully that solved the problem

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.