How do i make this script to work?

I want to a player get flinged upon death on R6. (I have already a ragdoll ready) the only thing left if that when they die they get flinged far away.

I have found this script upon searching online which seems to fit what i was looking for, but no luck after all, it doesn’t work. I tried putting it on ServerScriptService and StarterPlayerScripts and StarterChracterScripts, also its a server script.

game.Players.PlayerAdded:Connect(
	function(plr)
		plr.CharacterAdded:Connect(
			function(Character)


				local Humanoid = Character:FindFirstChild("Humanoid") -- Humanoid
				if Humanoid.Health = 0 then -- If the player is dead

					local BodyVelocity = Instance.new("BodyVelocity") -- Creates a new BodyVelocity force inside Character
					BodyVelocity.Parent = Character

					BodyVelocity.Velocity = Vector3.new(0, 200, 0) -- Sets velocity, you can change this up depending on what direction you want the player to go to in.

					wait(1)

					BodyVelocity:Destroy() -- Deletes the force, making the player start to come back down.
				end
			end
		)
	end
)
1 Like

I believe you’ll have to put this in a loop since it’s only checking if the player’s health is at 0 when they join the game. Do what the person under me is saying, actually.

For one, a portion of your code is not in correct lua syntax.

if Humanoid.Health = 0 then

This line should technically be this:

if Humanoid.Health == 0 then

Double equal signs indicates an operator.

However, you are checking if the player is dead upon loading the character, once. Instead, connect it to the death event.

game.Players.PlayerAdded:Connect(
	function(plr)
		plr.CharacterAdded:Connect(
			function(Character)
				local Humanoid = Character:FindFirstChild("Humanoid")

				Humanoid.Died:Connect(function()
					local BodyVelocity = Instance.new("BodyVelocity")

					BodyVelocity.Parent = Character:FindFirstChild("HumanoidRootPart")
					BodyVelocity.Velocity = Vector3.new(0, 200, 0)
					wait(1)
					BodyVelocity:Destroy()
				end)
			end)
	end)

Lastly, you set the BodyVelocity’s parent to the character, where a BodyVelocity is only useful whenever it is a child of a BasePart instance. Therefore, I changed the code so that it places it within the HumanoidRootPart instead.

Didn’t work when i tested it, with and without the ragdoll system

Try this…?

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")

		Humanoid.Died:Connect(function()
			local BodyVelocity = Instance.new("BodyVelocity")

			BodyVelocity.Parent = Character.HumanoidRootPart
			BodyVelocity.Velocity = Vector3.new(0, 200, 0)
			wait(1)
			BodyVelocity:Destroy()
		end)
	end)
end)

Nope, still nothing after all.

Are you getting any errors on the Output at all? This should be inside ServerScriptService

I’m not getting any errors on the output, and its on ServerScriptService

Wait I see why

Try setting the MaxForce property to math.huge

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")

		Humanoid.Died:Connect(function()
			local BodyVelocity = Instance.new("BodyVelocity")

			BodyVelocity.Parent = Character.HumanoidRootPart
			BodyVelocity.Velocity = Vector3.new(0, 200, 0)
            BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			wait(1)
			BodyVelocity:Destroy()
		end)
	end)
end)

Sadly i don’t see anything happening, (is this for r15? because im testing on r6) and also, im testing this on a baseplate.

It should work for both R6 & R15 since there are both HumanoidRootPart objects inside the Character

ACTUALLY NOW THAT I THINK ABOUT IT, the HumanoidRootPart’s CanCollide property is set by false on default I believe?

print("Script Online")

game.Players.PlayerAdded:Connect(function(plr)
    print("Player")
	plr.CharacterAdded:Connect(function(Character)
        print("Character")
		local Humanoid = Character:WaitForChild("Humanoid")

		Humanoid.Died:Connect(function()
            print("Ow")
			local BodyVelocity = Instance.new("BodyVelocity")

			BodyVelocity.Parent = Character.Torso
			BodyVelocity.Velocity = Vector3.new(0, 200, 0)
            BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			wait(1)
			BodyVelocity:Destroy()
		end)
	end)
end)

Actually, the script that Jackscarlett made IS working, but the issue is that the BodyVelocity is only being applied to the HumanoidRootPart. If you’d like to make the character actually get flinged when they die, the best thing to do would be to apply the BodyVelocity to ALL of the parts inside of the character, and not only the HumanoidRootPart.

It worked! Thank you so much! 30char)