Tween not working when player is ragdolled

I have a tween that works as it should. I tried to ragdoll the player when the tween starts so they get dragged out instead of just pulled through rocks and everything. However, the ragdoll stops the tween from moving the player.

This is my script:

local tweenService = game:GetService("TweenService")

local info = TweenInfo.new(5)
local goal = {CFrame = CFrame.new(-5.98, 11.44, 98.55)}

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local hrp = player.Character:FindFirstChild("HumanoidRootPart")
	if not player.Character:FindFirstChild("God") then
		game.ReplicatedStorage.Message:FireClient(player, "You are not welcome here!")
		local Animation = tweenService:Create(hrp, info, goal)
		Animation:Play()
	end
end)
1 Like

It looks like the issue you are experiencing is caused by the fact that the player’s character becomes a ragdoll while the tween is running, which will bug the movement of the tween.

One way to solve this issue would be to use a different technique to move the player’s character while the tween is running. One option could be to use the Humanoid:MoveTo function, which allows you to specify a target position for the character to move towards. You can use the TweenInfo object to specify the duration of the movement and other parameters, such as the easing function.

Here is an example of how you could fix your script to use the Humanoid:MoveTo function:

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local humanoid = player.Character:FindFirstChild("Humanoid")
local hrp = player.Character:FindFirstChild("HumanoidRootPart")

if not player.Character:FindFirstChild("God") then
    game.ReplicatedStorage.Message:FireClient(player, "You are not welcome here!")
    humanoid:MoveTo(CFrame.new(-5.98, 11.44, 98.55), hrp, info)
end
2 Likes

I’m going to try using MoveTo now, i’ll get back to you in a few minutes.

2 Likes

Wouldn’t it be more efficient to store the CFrame? (I know, premature optimization, but…)

local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local humanoid = player.Character:FindFirstChild("Humanoid")
local hrp = player.Character:FindFirstChild("HumanoidRootPart")
local goal = CFrame.new(-5.98, 11.44, 98.55)

if not player.Character:FindFirstChild("God") then
    game.ReplicatedStorage.Message:FireClient(player, "You are not welcome here!")
    humanoid:MoveTo(goal, hrp, info)
end
1 Like

In this case, storing the CFrame in a separate variable before using it in the Humanoid:MoveTo function would not have a significant impact on performance. Premature optimization, can often lead to unnecessary complexity in the script without providing a noticeable improvement in performance.

In my opinion it is generally a good practice to avoid repeating expensive operations whenever possible. In this case, calling the CFrame.new function every time the script runs would be an expensive operation, as it involves creating a new object and setting its properties. Storing the CFrame in a separate variable and reusing it would avoid this unnecessary overhead.

It is always a good idea to profile your script and identify the areas that are causing performance issues before attempting to optimize it. This will help ensure that any optimization efforts are focused on the areas of the code that will have the greatest impact on performance.

1 Like

Unfortunately, the player ragdolls but does not move.

there is an error in output though
image

Looks like Humanoid::MoveTo takes a Vector3, so just replace CFrame with Vector3.

Same issue, however the error changes.

They ragdoll but don’t move.

Error:

image

error is on line 7 which is this line

local hrp = player.Character:FindFirstChild("HumanoidRootPart")

Is player an actual player? It looks like the thing that is receiving the touch isn’t a direct child of the player’s model. Try replacing hit.Parent on the first line with hit:FindFirstAncestorOfClass("Model") to guarantee that the script uses the in-world character.

this is my script, it has hit.Parent to find the player from their character

local tweenService = game:GetService("TweenService")

local info = TweenInfo.new(5)

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local hrp = player.Character:FindFirstChild("HumanoidRootPart")
	if not player.Character:FindFirstChild("God") then
		player.Character:FindFirstChild("RagdollR15").Activate.Value = true
		game.ReplicatedStorage.Message:FireClient(player, "You are not welcome here!")
		player.Character:FindFirstChild("Humanoid"):MoveTo(Vector3.new(-5.98, 11.44, 98.55), hrp, info)
		wait(8)
		player.Character:FindFirstChild("RagdollR15").Activate.Value = false
	end
end)

Then use that in place of hit.Parent on line 6. Using FindFirstAncestorOfClass will guarantee that it returns the character’s root model instead of the parent of potential nested parts.

i dont quite understand what you mean, could you expand please?

I’m asking you to replace hit.Parent in this line with hit:FindFirstAncestorOfClass("Model"). This should fix your issue.

All characters are Models. This replacement ensures that you always get the player’s character before you try to find the actual player from it.

That stops the character error. There is now nothing in output, however the character still does not move.

Ragdolled humanoids can’t move, but I don’t know how to fix this. I would try creating a temporary BodyPosition, setting the Position property to your target position, setting the (badly-named) P property to a high enough number, and removing it when it finishes:

local bp = Instance.new("BodyPosition")
bp.Position = Vector3.new(-5.98, 11.44, 98.55)
bp.P = 500 -- increase if you need to
bp.ReachedTarget:Once(function()
    bp:Destroy()
    -- add any code you want to run when the humanoid is done moving
end)
bp.Parent = hrp

i’ll try this now, thank you :slight_smile:

This still doesn’t move them
and they dont un ragdoll because it never reaches the target

I don’t know then. If you click on the HumanoidRootPart in the explorer, does it show it moving? IIRC moving the HumanoidRootPart forces the entire Humanoid to move, but maybe something isn’t welded properly.

i think i know why.

i selected the humanoid root part in explorer, and when i ragdolled, the humanoidrootpart was deleted.

this is an issue with my ragdoll script. do you have any ragdoll scripts i could use, any alternative ways of ragdolling a player, or any tutorials on a ragdoll script?