Script doesn't detect torso

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I need help debugging this script, it detects when the player is in ragdoll, then it will play a sound when the torso is touched.

  2. What is the issue? Include screenshots / videos if possible!
    The touched function in particular doesnt seem to be working

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried waiting for the sound to stop before adding it to debris, I’ve tried printing strings to help find when the Torso touched event is used. I’ve also tried looking at other forum posts but they don’t rlly help my case.


local char = script.Parent
local torso = char:WaitForChild("Torso")
local impactSounds = script:WaitForChild("Impact")  

local function onTorsoTouched(part)
	if part:IsA("BasePart") and not part:IsDescendantOf(char) then
		if char:FindFirstChild("Ragdoll") then
			local impactSound = impactSounds:GetChildren()[math.random(1, #impactSounds:GetChildren())]
			local soundClone = impactSound:Clone()
			soundClone.Parent = torso
			soundClone.Volume = 1
			soundClone.PlaybackSpeed = 1
			soundClone:Play()
			game.Debris:AddItem(soundClone, soundClone.TimeLength)
		end

	end

end

torso.Touched:Connect(onTorsoTouched)

any help would be appreciated, thanks.

1 Like

Add the line: if script.Parent == game.StarterPlayer.StarterCharacterScripts then return end

tried this but to no avail :frowning:

local char = script.Parent
local torso = char:WaitForChild("Torso")
local impactSounds = script:WaitForChild("Impact")  

local function onTorsoTouched(part)
	
	if script.Parent == game.StarterPlayer.StarterCharacterScripts then return end
	
	if part:IsA("BasePart") and not part:IsDescendantOf(char) and char:FindFirstChild("Ragdoll") then
			local impactSound = impactSounds:GetChildren()[math.random(1, #impactSounds:GetChildren())]
			local soundClone = impactSound:Clone()
			soundClone.Parent = torso
			soundClone.Volume = 1
			soundClone.PlaybackSpeed = 1
			soundClone:Play()
			game.Debris:AddItem(soundClone, soundClone.TimeLength)
	end

end

torso.Touched:Connect(onTorsoTouched)

Sorry, forgot to include that it needs to be before anything, at line 1

tried this to but still the same outcome, anything else?

Torso could be a mesh part?

Is the actual function running? Try adding some print statements to see where the issue lies.
If the function isn’t even running then the issue is with the .Touched event, and I’m pretty sure that’s the case since .Touched only works with physical movement (which I don’t fully understand yet).

For testing purposes, try making something else trigger the function and see what happens.

yeah, the touch function does run, but i noticed it only runs when the player is actually alive and not in ragdoll

if script.Parent == game.StarterPlayer.StarterCharacterScripts then return end

local char = script.Parent
local torso = char:WaitForChild("Torso")
local impactSounds = script:WaitForChild("Impact")  

local function onTorsoTouched(part)
	print("Function running")
	
	if part:IsA("BasePart") and not part:IsDescendantOf(char) and char:FindFirstChild("Ragdoll") then
			local impactSound = impactSounds:GetChildren()[math.random(1, #impactSounds:GetChildren())]
			local soundClone = impactSound:Clone()
			soundClone.Parent = torso
			soundClone.Volume = 1
			soundClone.PlaybackSpeed = 1
			soundClone:Play()
			game.Debris:AddItem(soundClone, soundClone.TimeLength)
	end

end

torso.Touched:Connect(onTorsoTouched)

Try detecting HumanoidRootPart instead

still nothing, again the function works fine when the player is alive, but not when theyve died. maybe the function cant be called on a dead character? idk

Try this code:


local char = script.Parent
local torso = char:WaitForChild("Torso")
local humanoid=char:WaitForChild("Humanoid")
local impactSounds = script:WaitForChild("Impact")  

local function onTorsoTouched(part)
	print("Function running")
	
	if part:IsA("BasePart") and not part:IsDescendantOf(char) and humanoid:GetState()==Enum.HumanoidStateType.Dead then
			local impactSound = impactSounds:GetChildren()[math.random(1, #impactSounds:GetChildren())]
			local soundClone = impactSound:Clone()
			soundClone.Parent = torso
			soundClone.Volume = 1
			soundClone.PlaybackSpeed = 1
			soundClone:Play()
			game.Debris:AddItem(soundClone, soundClone.TimeLength)
	end

end

torso.Touched:Connect(onTorsoTouched)

You may have to check humanoid state to check if there dead since im pretty sure roblox does check ragdoll state type anymore

this dooesn’t work either, but what someone else told me is it probably has something to do with network ownership

You replacing the torso when you change into ragdoll?

Ended up fixing it by just handling everything on the server. Thanks everyone for helping!

Next time tell us that the script you’re using is a Local script instead of making us assume its a server script :d

Well, you can see the indications it was a local script (script.Parent at the end)

the first line literally indicates its a local script…

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