so basically i am trying to make an realistic ragdoll
Assistant
Okay, to create a realistic ragdoll in Roblox, you can use the following code:
code
local Ragdoll = {}
Ragdoll.__index = Ragdoll
function Ragdoll.new(character)
local self = setmetatable({}, Ragdoll)
self.humanoid = character:WaitForChild("Humanoid")
self.humanoidRootPart = character:WaitForChild("HumanoidRootPart")
self.originalAnchored = self.humanoidRootPart.Anchored
self.originalMass = self.humanoidRootPart:GetMass()
self.humanoid.Died:Connect(function()
self:TurnOnRagdoll()
end)
return self
end
function Ragdoll:TurnOnRagdoll()
self.humanoid:ChangeState(Enum.HumanoidStateType.Physics)
end
function Ragdoll:TurnOffRagdoll()
self.humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
wait(0.5)
self.humanoidRootPart.Anchored = self.originalAnchored
self.humanoid:ChangeState(Enum.HumanoidStateType.Seated)
end
function Ragdoll:SetRagdollMass(mass)
if self.humanoidRootPart.Parent == nil then
return
end
self.humanoidRootPart.Massless = true
self.humanoidRootPart:SetNetworkOwner(nil)
self.humanoidRootPart:SetMassProperties(mass, self.humanoidRootPart.AssemblyCenter, true)
end
function Ragdoll:SetHumanoidMass(mass)
self.humanoidRootPart.Massless = false
self.humanoidRootPart:SetNetworkOwner(self.humanoid)
self.humanoidRootPart:SetMassProperties(mass, self.humanoidRootPart.AssemblyCenter, true)
end
function Ragdoll:SetRagdollJoints()
local joints = self.humanoidRootPart:GetChildren()
for _, joint in pairs(joints) do
if joint:IsA("JointInstance") then
joint.C0 = joint.C0 * CFrame.new(0, -1000, 0)
joint.C1 = joint.C1 * CFrame.new(0, -1000, 0)
end
end
end
function Ragdoll:EnableRagdoll()
self:SetRagdollMass(self.originalMass)
self:SetRagdollJoints()
self:TurnOnRagdoll()
end
function Ragdoll:DisableRagdoll()
self:TurnOffRagdoll()
self:SetHumanoidMass(self.originalMass)
end
return Ragdoll
Here is how you can use this code in your game:
Place the above code in a ModuleScript in your game.
Require the ModuleScript at the top of your script:
code
local RagdollModule = require(script.Parent.RagdollModule)
Create a Ragdoll object for each character once they spawn:
code
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local ragdoll = RagdollModule.new(character)
end)
end)
Call the ragdoll:EnableRagdoll() method when the character dies:
code
ragdoll:EnableRagdoll()
Call the ragdoll:DisableRagdoll() method when the character respawns:
code
ragdoll:DisableRagdoll()
This will create a realistic ragdoll effect for the character. Note that this code is just an example and can be modified as per your requirements.
User
excuse me but, i am doing it so it doesnt trigger when the client dies, im doing it so it triggers with an event, not a keybind or mouse click
Assistant
In that case, you can modify the Ragdoll module to handle events for turning on and off the ragdoll effect. Here's an example:
code
local Ragdoll = {}
Ragdoll.__index = Ragdoll
function Ragdoll.new(character)
local self = setmetatable({}, Ragdoll)
self.humanoid = character:WaitForChild("Humanoid")
self.humanoidRootPart = character:WaitForChild("HumanoidRootPart")
self.originalAnchored = self.humanoidRootPart.Anchored
self.originalMass = self.humanoidRootPart:GetMass()
self.humanoid.Died:Connect(function()
self:TurnOnRagdoll()
end)
return self
end
function Ragdoll:TurnOnRagdoll()
self.humanoid:ChangeState(Enum.HumanoidStateType.Physics)
end
function Ragdoll:TurnOffRagdoll()
self.humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
wait(0.5)
self.humanoidRootPart.Anchored = self.originalAnchored
self.humanoid:ChangeState(Enum.HumanoidStateType.Seated)
end
function Ragdoll:SetRagdollMass(mass)
if self.humanoidRootPart.Parent == nil then
return
end
self.humanoidRootPart.Massless = true
self.humanoidRootPart:SetNetworkOwner(nil)
self.humanoidRootPart:SetMassProperties(mass, self.humanoidRootPart.AssemblyCenter, true)
end
function Ragdoll:SetHumanoidMass(mass)
self.humanoidRootPart.Massless = false
self.humanoidRootPart:SetNetworkOwner(self.humanoid)
self.humanoidRootPart:SetMassProperties(mass, self.humanoidRootPart.AssemblyCenter, true)
end
function Ragdoll:SetRagdollJoints()
local joints = self.humanoidRootPart:GetChildren()
for _, joint in pairs(joints) do
if joint:IsA("JointInstance") then
joint.C0 = joint.C0 * CFrame.new(0, -1000, 0)
joint.C1 = joint.C1 * CFrame.new(0, -1000, 0)
end
end
end
function Ragdoll:EnableRagdoll()
self:SetRagdollMass(self.originalMass)
self:SetRagdollJoints()
self:TurnOnRagdoll()
end
function Ragdoll:DisableRagdoll()
self:TurnOffRagdoll()
self:SetHumanoidMass(self.originalMass)
end
return Ragdoll
User
i have got some scripts ready, i just need for you to check if they're correct
Assistant
Sure, I would be happy to check your scripts and provide feedback. Please share your code with me.
User
so i got in RagdollModuleScript:
local RagdollEnabled = false
local function toggleRagdoll(player)
local character = player.Character
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
if RagdollEnabled then
humanoid:ChangeState(Enum.HumanoidStateType.Physics)
for _, part in pairs(character:GetDescendants()) do
if part:IsA("BasePart") then
part.Anchored = false
local bodyforce = part:FindFirstChildOfClass("BodyForce")
if bodyforce then
bodyforce:Destroy()
end
end
end
RagdollEnabled = false
else
humanoid:ChangeState(Enum.HumanoidStateType.Physics)
for _, part in pairs(character:GetDescendants()) do
if part:IsA("BasePart") then
local joint = Instance.new("Motor6D")
joint.Name = "RagdollJoint"
joint.Part0 = humanoid.RootPart
joint.Part1 = part
joint.C0 = part.CFrame:inverse() * humanoid.RootPart.CFrame
joint.C1 = CFrame.new()
joint:Initialize()
joint.Parent = humanoid.RootPart
code
part.Anchored = false
local bodyforce = Instance.new("BodyForce", part)
local weight = part:GetMass()
bodyforce.Force = Vector3.new(0, weight * 600, 0)
game:GetService("Debris"):AddItem(joint, 1)
game:GetService("Debris"):AddItem(bodyforce, 1)
end
end
RagdollEnabled = true
end
end
end
return {
toggleRagdoll = toggleRagdoll
}
on localscript i got:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RagdollModule = require(ReplicatedStorage:WaitForChild("RagdollModule"))
local button = script.Parent
button.MouseButton1Click:Connect(function()
local character = game.Players.LocalPlayer.Character
if character then
ReplicatedStorage.ToggleRagdoll:FireServer(game.Players.LocalPlayer)
end
end)
and on serverscript i got:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RagdollModule = require(ReplicatedStorage:WaitForChild("RagdollModule"))
Now, how do i make it trigger with an event on another code? enabling it and disabling it with just an enable and disable functions?
Assistant
To trigger the ragdoll effect with an event on another code, you can define separate functions for enabling and disabling the ragdoll effect in the RagdollModuleScript. Here's an example:
code
local RagdollEnabled = false
local function enableRagdoll(player)
local character = player.Character
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid and not RagdollEnabled then
humanoid:ChangeState(Enum.HumanoidStateType.Physics)
for _, part in pairs(character:GetDescendants()) do
if part:IsA("BasePart") then
local joint = Instance.new("Motor6D")
joint.Name = "RagdollJoint"
joint.Part0 = humanoid.RootPart
joint.Part1 = part
joint.C0 = part.CFrame:inverse() * humanoid.RootPart.CFrame
joint.C1 = CFrame.new()
joint:Initialize()
joint.Parent = humanoid.RootPart
part.Anchored = false
local bodyforce = Instance.new("BodyForce", part)
local weight = part:GetMass()
bodyforce.Force = Vector3.new(0, weight * 600, 0)
game:GetService("Debris"):AddItem(joint, 1)
game:GetService("Debris"):AddItem(bodyforce, 1)
end
end
RagdollEnabled = true
end
end
local function disableRagdoll(player)
local character = player.Character
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid and RagdollEnabled then
humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
wait(0.5)
humanoid.RootPart.Anchored = true
humanoid:ChangeState(Enum.HumanoidStateType.Seated)
for _, part in pairs(character:GetDescendants()) do
if part:IsA("Motor6D") and part.Name == "RagdollJoint" then
part:Destroy()
end
if part:IsA("BasePart") then
part.Anchored = true
local bodyforce = part:FindFirstChildOfClass("BodyForce")
if bodyforce then
bodyforce:Destroy()
end
end
end
RagdollEnabled = false
end
end
return {
enableRagdoll = enableRagdoll,
disableRagdoll = disableRagdoll
}
User
ok, i got the code, now, do i put it in a remote event?
Assistant
Yes, that's correct! You can put the code inside a RemoteEvent to trigger it from another script. Here's an example of a RemoteEvent implementation:
First, create a new RemoteEvent in ReplicatedStorage:
code
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local toggleRagdollEvent = Instance.new("RemoteEvent")
toggleRagdollEvent.Name = "ToggleRagdollEvent"
toggleRagdollEvent.Parent = ReplicatedStorage
Inside the code where you want to trigger the ragdoll effect, you can fire the RemoteEvent with the player as a parameter:
code
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local toggleRagdollEvent = ReplicatedStorage.ToggleRagdollEvent
-- Trigger ragdoll effect for the player
toggleRagdollEvent:FireServer(player)
In the server script, you can listen for the RemoteEvent and call the enableRagdoll and disableRagdoll functions as needed:
code
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local toggleRagdollEvent = ReplicatedStorage.ToggleRagdollEvent
local RagdollModule = require(ReplicatedStorage:WaitForChild("RagdollModule"))
toggleRagdollEvent.OnServerEvent:Connect(function(player)
if RagdollEnabled then
RagdollModule.disableRagdoll(player)
else
RagdollModule.enableRagdoll(player)
end
end)
Done! Now, whenever you fire the ToggleRagdollEvent RemoteEvent, it will trigger the enableRagdoll or disableRagdoll function depending on the current state.
User
do i like, put the three scripts inside the remote event or just the module one?
Assistant
You only need to put the RagdollModuleScript in ReplicatedStorage so that it can be accessed by both the client and server scripts. The ToggleRagdollEvent RemoteEvent and the code to fire or listen for it should be in separate scripts, as shown in the example above.
So you will have three separate scripts as given below:
A RagdollModuleScript containing the code for enabling and disabling the ragdoll effect.
A client-side script that fires the ToggleRagdollEvent RemoteEvent when the player triggers the ragdoll effect.
A server-side script that listens for the ToggleRagdollEvent RemoteEvent and calls the enableRagdoll or disableRagdoll function depending on the current state.
User
Ok, now i would like to test it first, when i press the play button i want it to trigger immediately
Assistant
Sure, you can achieve this by adding the following code to your client-side script:
code
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local toggleRagdollEvent = ReplicatedStorage.ToggleRagdollEvent
local RagdollModule = require(ReplicatedStorage:WaitForChild("RagdollModule"))
-- Fire the ToggleRagdollEvent immediately when the game starts
toggleRagdollEvent:FireServer(game.Players.LocalPlayer)
-- Listen for the ToggleRagdollEvent
toggleRagdollEvent.OnClientEvent:Connect(function(player)
-- Code to execute when the ragdoll effect is triggered
end)
This will immediately fire the ToggleRagdollEvent RemoteEvent when the game starts and trigger the ragdoll effect for the local player. You can customize the code to execute when the event is triggered.
User
the client side script its the localscript right?
Assistant
Yes, that's correct! In Roblox, LocalScript objects are used to handle client-side scripting. So you should put the client-side code that triggers the ragdoll effect in a LocalScript. The server-side code that listens for the RemoteEvent should be in a separate Script or LocalScript that is parented to the ServerScriptService.
User
do i only change the localscript right?
Assistant
Yes, you only need to modify the client-side LocalScript that fires the ToggleRagdollEvent RemoteEvent. Here's an example:
code
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local toggleRagdollEvent = ReplicatedStorage.ToggleRagdollEvent
local RagdollModule = require(ReplicatedStorage:WaitForChild("RagdollModule"))
-- Fire the ToggleRagdollEvent immediately when the game starts
toggleRagdollEvent:FireServer(game.Players.LocalPlayer)
-- Listen for the ToggleRagdollEvent
toggleRagdollEvent.OnClientEvent:Connect(function(player)
-- Code to execute when the ragdoll effect is triggered
end)
This code will fire the ToggleRagdollEvent RemoteEvent immediately when the game starts and trigger the ragdoll effect for the local player. You can customize the code to execute when the event is triggered.
In your main script, you can create an Event object to represent the event that triggers the ragdoll effect:
code
local ragdollEvent = Instance.new("BindableEvent")
Then, you can listen for the event and enable the ragdoll effect when it's triggered:
code
local RagdollModule = require(script.Parent.RagdollModule)
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local ragdoll = RagdollModule.new(character)
ragdollEvent.Event:Connect(function()
ragdoll:EnableRagdoll()
end)
end)
end)
When you want to trigger the ragdoll effect, you can simply call ragdollEvent:Fire() and it will enable the ragdoll effect for the player's character.
You can also disable the ragdoll effect by calling ragdoll:DisableRagdoll() when the character respawns.
Then, in your main script where you want to trigger the ragdoll effect, you can require the RagdollModuleScript and call the enableRagdoll and disableRagdoll functions as needed. Here's an example:
code
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RagdollModule = require(ReplicatedStorage:WaitForChild("RagdollModule"))
-- Call this function to enable the ragdoll effect
local function enableRagdoll(player)
RagdollModule.enableRagdoll(player)
end
-- Call this function to disable the ragdoll effect
local function disableRagdoll(player)
RagdollModule.disableRagdoll(player)
end
You can customize these functions as per your requirements. Finally, you can trigger the ragdoll effect by calling enableRagdoll(player) and disable it using disableRagdoll(player) where player is the player object on which you want to apply the effect.