Trying to detect what player touched who first

i have this ragdoll script that should ragdoll the other player when u touch them. the thing is tho is that its also ragdolling my character as well.

vid:

script:

local db = false


game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char.PrimaryPart = char:WaitForChild("HumanoidRootPart")
		
		for i, part in pairs(char:GetChildren()) do
			if part:IsA("BasePart") or part:IsA("MeshPart") then
				part.Touched:Connect(function(hit)
					if db then return end

					local hum = hit.Parent:FindFirstChild("Humanoid")
					local otherPlr = hit.Parent

				local primarypart = hit.Parent.PrimaryPart
				local mass = 0
				
					if hum and not hit:IsDescendantOf(char) then
						local anim = script.Animation
						local animtrack = hum.Animator:LoadAnimation(anim)
						animtrack:Play()



						hum.BreakJointsOnDeath = false
						
						if animtrack.IsPlaying then
							wait(.5)
						end

						
						for i, joint in pairs(otherPlr:GetDescendants()) do
							if joint:IsA("Motor6D") then
								local socket:BallSocketConstraint = Instance.new("BallSocketConstraint", joint.Parent)
								local a1:Attachment = Instance.new("Attachment", joint.Part0)
								local a2:Attachment = Instance.new("Attachment", joint.Part1)
								socket.Attachment0 = a1
								socket.Attachment1 = a2
								socket.UpperAngle = -180
								socket.Restitution = 10000
								socket.MaxFrictionTorque = 50
								a1.CFrame = joint.C0
								a2.CFrame = joint.C1
								socket.LimitsEnabled = true
								socket.TwistLimitsEnabled = true
								a1.SecondaryAxis = Vector3.new(0,0,20)
								

								
								joint:Destroy()
								
							end
						end
					end
				end)
			end
		end
		
	end)
end)
2 Likes

Set the Humanoid’s PlatformStand property to true on ragdoll.

1 Like

Sorry, I have no idea why the rest of my reply didn’t register. I tried editing it immediately after I posted it (I accidentally replied too early, so I assumed the edit applied) but it didn’t. To answer your question, it looks like both players are touching eachother at the same time so they both ragdoll. Try add a check to make sure that the other player is not ragdolled before attempting a ragdoll.

1 Like

hello sausages!

tysm for ur reply. theres np at all. i tried the platformstand property and its actually quite cool tbf.

→ it looks like both players are touching eachother at the same time so they both ragdoll

ur absolutely right but im not quite sure how to deal with this.

this part basically deals w everything: :slight_smile:

				part.Touched:Connect(function(hit)
					if db then return end
					local hum = hit.Parent:FindFirstChild("Humanoid")
					local otherPlr = hit.Parent
					local primarypart = hit.Parent.PrimaryPart
					local mass = 0
				
					if hum and not hit:IsDescendantOf(char) then

	
						local anim = script.Animation
						local animtrack = hum.Animator:LoadAnimation(anim)

						animtrack:Play()
							hum.Jump = true


						
						task.wait(.1)
						
hum.PlatformStand = true
						
						--ragdoll
						for i, joint in pairs(otherPlr:GetDescendants()) do
							if joint:IsA("Motor6D") then
								local socket:BallSocketConstraint = Instance.new("BallSocketConstraint", joint.Parent)
								local a1:Attachment = Instance.new("Attachment", joint.Part0)
								local a2:Attachment = Instance.new("Attachment", joint.Part1)
								socket.Attachment0 = a1
								socket.Attachment1 = a2
								socket.UpperAngle = -180
								socket.Restitution = 10000
								socket.MaxFrictionTorque = 50
								a1.CFrame = joint.C0
								a2.CFrame = joint.C1
								socket.LimitsEnabled = true
								socket.TwistLimitsEnabled = true
								a1.SecondaryAxis = Vector3.new(0,0,20)
								

								
								joint:Destroy()
								
							end
						end
					end
				end)
			end
		end
		
	end)
end)

Ah, wait, it looks like you just never apply debounce. While I’m on this topic, I noticed that you control ‘db’ on the server. This means that if one person collides with another person, nobody else could ever collide until said debounce is reset.

1 Like

this isn’t the right way to go about making this.
scrap the serverscript and make it handled on the client, this puts less load on the server and produces more consistent results, fire a remote event when localplayer touches another player, make some sanity checks and you should be good.

1 Like

The ragdoll has to happen on the client and failure to do so can create some pretty interesting physics.
But as far as your question is concerned, the .Touched event is going to fire within milliseconds and is going to apply to both characters. My advice would be to create some sort of action a player can do, such as sticking their arm out like a game of tag (Guessing this is like what you’re trying to do?), then adding a hitbox to said arm that will fire the .Touched event (and only that hitbox, not just every player’s appendage) otherwise, regular collisions will just make both players ragdoll.

1 Like

@VerySillySausages @karlisjarl @Xeau

hey guys, tysm 4 ur help i really appreciate it. i optimized my scripts a little better towards karl’s way if that makes sense however, im not quite sure on how to prevent both players from being affected. im also trying hitboxes rn, got a script for that but is there any way to make it so that the player who touched the other doesnt get affected?

localscript:

local db = false
local char = script.Parent
local event = game.ReplicatedStorage:WaitForChild("plrRagdoll")
	
			for i, part in pairs(char:GetChildren()) do
				if part:IsA("BasePart") or part:IsA("MeshPart") then
					part.Touched:Connect(function(hit)
					if db then return end	
					db = false
							local hum = hit.Parent:FindFirstChild("Humanoid")
							local otherPlr = hit.Parent
							local primarypart = hit.Parent.PrimaryPart
							local mass = 0
							if hum and not hit:IsDescendantOf(char) then
								print("is plr")
								event:FireServer(hum)
								db = true
								wait(1)
								db = false
							end
					end)
				end
			end

script:

local event = game.ReplicatedStorage:WaitForChild("plrRagdoll")

event.OnServerEvent:Connect(function(plr, hum: Humanoid)
	local anim = script.Animation
	local animtrack = hum.Animator:LoadAnimation(anim)
	local otherPlr = hum.Parent

	animtrack:Play()
	hum.Jump = true
	
	
task.wait(.1)

hum.PlatformStand = true

--ragdoll
for i, joint in pairs(otherPlr:GetDescendants()) do
	if joint:IsA("Motor6D") then
		local socket:BallSocketConstraint = Instance.new("BallSocketConstraint", joint.Parent)
		local a1:Attachment = Instance.new("Attachment", joint.Part0)
		local a2:Attachment = Instance.new("Attachment", joint.Part1)
		socket.Attachment0 = a1
		socket.Attachment1 = a2
		socket.UpperAngle = -180
		socket.Restitution = 10000
		socket.MaxFrictionTorque = 50
		a1.CFrame = joint.C0
		a2.CFrame = joint.C1
		socket.LimitsEnabled = true
		socket.TwistLimitsEnabled = true
		a1.SecondaryAxis = Vector3.new(0,0,20)



		joint:Destroy()
		
		end
	end
	end)

the server script needs to check if the player trying to ragdoll another player is currently ragdolled or not, this ensures that only 1 person will get ragdolled.

my recommended way of doing this is to set an attribute in the character of the person who needs to get ragdolled. when the remote is fired, check if said attribute is true, if it is then ignore the call, if not then continue on with the code.

1 Like

hello karl,
ur feedback is brilliant. i made an attribute and set the ragdolled attribute to true if the player touched another player. its working great, i just need to get the checks right so that it wont run for both players. how would u do the checks for ignoring the player who touched the other? tysm :slight_smile:

localscript:

local char = script.Parent
local event = game.ReplicatedStorage:WaitForChild("plrRagdoll")



for i, part in pairs(char:GetChildren()) do
	if part:IsA("BasePart") or part:IsA("MeshPart") then
		part.Touched:Connect(function(hit)
			local charisRagdolled = char:GetAttribute("ragdolled", true)
			if charisRagdolled then return end
			local hum = hit.Parent:FindFirstChild("Humanoid")

			if hum and not hum:IsDescendantOf(char) then
				local otherChar = hit.Parent
				local PlrragdollAttribute = otherChar:GetAttribute("ragdolled")
				if PlrragdollAttribute == false then
local ragdolled = otherChar:SetAttribute("ragdolled", true)

event:FireServer(hum)

				end
			end
		end)
	end
end

script:

local event = game.ReplicatedStorage:WaitForChild("plrRagdoll")


event.OnServerEvent:Connect(function(plr, hum: Humanoid)
	if hum then
	local anim = script.Animation
	local animtrack = hum.Animator:LoadAnimation(anim)
	local otherPlr = hum.Parent

	animtrack:Play()
	hum.Jump = true
	
	
task.wait(.1)

hum.PlatformStand = true

--ragdoll
for i, joint in pairs(otherPlr:GetDescendants()) do
	if joint:IsA("Motor6D") then
		local socket:BallSocketConstraint = Instance.new("BallSocketConstraint", joint.Parent)
		local a1:Attachment = Instance.new("Attachment", joint.Part0)
		local a2:Attachment = Instance.new("Attachment", joint.Part1)
		socket.Attachment0 = a1
		socket.Attachment1 = a2
		socket.UpperAngle = -180
		socket.Restitution = 10000
		socket.MaxFrictionTorque = 50
		a1.CFrame = joint.C0
		a2.CFrame = joint.C1
		socket.LimitsEnabled = true
		socket.TwistLimitsEnabled = true
		a1.SecondaryAxis = Vector3.new(0,0,20)


		joint:Destroy()
	else return
		end
		end
	end
	end)
1 Like

you’ll have to set and check the attribute on the server, as the client’s changes do not replicate for other players. run this check on the server:

if otherPlr:GetAttribute("ragdolled") then return end
otherPlr:SetAttribute("ragdolled", true)

once you want to unragdoll the other player, set the attribute to false like so:

otherPlr:SetAttribute("ragdolled", false)
1 Like

karl u absolute legend. works like a billion dollars thats mad haha. it all makes sense to me now. tysm 4 spending the time to help me i really appreciate it. have a great day :smiley:

1 Like

no worries, it’s simple stuff for me.

1 Like

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