Getting stuck in ragdoll need help

I have a pushing tool in my game and inside the tool I have a script that lets the pushing system work and it works fine but if 2 people push at the same time and one goes off the map and dies while the one on the map is ragdolled they don’t unragdoll not sure why it does this but any help is appreciated, thanks.

Script

wait(0.002)
local Tool = script.Parent
local Figure = Tool.Parent
local equipped = false
local db = true
local canPush = false
repeat wait(0.001) until script.Parent.Parent:FindFirstChild("Humanoid")
local h = Tool.Parent:FindFirstChild("Humanoid")
--[ Stats ]--
local Cooldown = Tool.Configuration.Cooldown
--[ Script ]--
function onActive()
	if db == true and h then
		db = false
		canPush = true
		local pushAnim = h:LoadAnimation(Tool.PushAnim)
		pushAnim:Play()
		Tool.Handle.Touched:Connect(function(hit)
			local hitHuman = hit.Parent:FindFirstChild("Humanoid")
			if hitHuman and canPush == true and pushAnim.IsPlaying and not Tool.Parent.Effects:FindFirstChild("Ragdoll") then
				canPush = false
				Tool.Handle.Hit:Play()
					local bv = Instance.new("BodyVelocity")
					local offset = Vector3.new()
					bv.Parent = (hitHuman.Parent.HumanoidRootPart)
					bv.MaxForce = Vector3.new(10000000,10000000,10000000)
					bv.Velocity = h.Parent.Head.CFrame.LookVector * 55
					wait(0.01)
					bv:Destroy()
				if hit.Parent:FindFirstChild("Effects") then
					if not hit.Parent.Effects:FindFirstChild("Ragdoll") then
						local z = Instance.new("BoolValue", hit.Parent.Effects)
						z.Name = "Ragdoll"
						wait(3)
						z:Destroy()
					end
				end
			end
		end)
		wait(Cooldown.Value)
		canPush = false
		db = true
	end
end

Tool.Activated:Connect(onActive)

Video Of It Happening

Use debris service instead so you don’t need to yield the script

local Debris = game:GetService("Debris")
Debris:AddItem(z, 3) 
task.wait(0.1)
1 Like

This may or may not be related, but if not, here’s a tip. As per the documentation for Instance, you shouldn’t use the parent optional field.

Performance note: When the Parent of an object is set, Luau listens to a variety of different property changes for replication, rendering and physics. Therefore, it is recommended to set the Parent property last when creating new objects. As such, you should avoid using the second argument (parent) of this function. You can read this thread on the developer forum for more information.

More on this here:

Once an object is attached to game , a lot of internal ROBLOX systems start listening to property changes on the object and updating various internal data structures for the change to take effect. These updates can involve queueing changes for replication, updating physics contact state, queueing rendering state changes etc.

It’s thus important that when you create the object, the initial object field assignment is correctly classified as “initial setup” as opposed to “changing state of the existing object” - and the differentiator for this is .Parent, or rather the object being a descendant of game . For this reason, .Parent assignment should go after all property assignments during the object creation. You most likely want to connect all signals after that to make sure they are not dispatched during creation - so the optimal sequence is:

A. Instance.new
B. Assign properties
C. Assign Parent
D. Connect signals

1 Like

thanks this is good to know im gonna edit this script

1 Like

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