I have ragdoll script that creates BoolValue inside Character and changes character state from not ragdoll to ragdoll and so on. When i started testing it by changing BoolValue value with mouse clicker (very fast) turns out it is not firing sometimes and causes glitches
local events = game:GetService("ReplicatedStorage"):WaitForChild("Events").Player
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local ragdoll = Instance.new("BoolValue", char)
ragdoll.Name = "ragdoll"
ragdoll.Value = false
local cool = false
joints = {} welds = {} fakeLimbs = {}
ragdolling = false
ragdoll.Changed:Connect(function(newVal)
local ragdollChar = coroutine.wrap(function(char)
events:WaitForChild("RagdollCameraOn"):FireClient(player)
char.Humanoid.JumpPower = 0
char.Humanoid.Jump = false
ragdolling = true
wait()
char.Humanoid.PlatformStand = true
local leftArm = char:FindFirstChild("Left Arm")
local rightArm = char:FindFirstChild("Right Arm")
local leftLeg = char:FindFirstChild("Left Leg")
local rightLeg = char:FindFirstChild("Right Leg")
local head = char:WaitForChild("Head")
local torso = char:WaitForChild("Torso")
local function cloneJoints(character)
joints = {}
for _,this in ipairs(character.Torso:GetChildren()) do
if this:isA("Motor6D") and this.Name ~= "Neck" then
joints[this.Name] = {this,this.Part0}
end
end
end
local function createGlue(part0, part1, name)
local glue = Instance.new("Glue", torso)
glue.Part0 = part0
glue.Part1 = part1
glue.Name = name
welds[#welds+1] = glue
return glue
end
local function createLimb(parent, position, size)
local limb = Instance.new("Part", parent)
limb.Position = position;limb.Size = size;limb.Transparency = 1
fakeLimbs[#fakeLimbs+1] = limb
return limb
end
local function createWeld(parent, p0, p1)
local weld = Instance.new("Weld", parent)
weld.Part0 = p0
weld.Part1 = p1
weld.C0 = CFrame.new(0,-0.2,0) * CFrame.fromEulerAnglesXYZ(0, 0, math.pi/2)
welds[#welds+1] = weld
return weld
end
cloneJoints(char)
-- LEFT LEG --
if char.Torso:FindFirstChild("Left Hip") and leftLeg then
char.Torso:WaitForChild("Left Hip").Part0 = nil
local glue = createGlue(torso, leftLeg, "Left leg")
glue.C0 = CFrame.new(-0.5, -1, 0, -0, -0, -1, 0, 1, 0, 1, 0, 0)
glue.C1 = CFrame.new(-0, 1, 0, -0, -0, -1, 0, 1, 0, 1, 0, 0)
local fakeLeftLeg = createLimb(leftLeg, Vector3.new(0,999,0), Vector3.new(1.5, 1, 1))
createWeld(fakeLeftLeg, leftLeg, fakeLeftLeg)
end
-- RIGHT LEG --
if char.Torso:FindFirstChild("Right Hip") and rightLeg then
char.Torso:WaitForChild("Right Hip").Part0 = nil
local glue = createGlue(torso, rightLeg, "Right leg")
glue.C0 = CFrame.new(0.5, -1, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0)
glue.C1 = CFrame.new(0, 1, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0)
local fakeRightLeg = createLimb(rightLeg, Vector3.new(0,999,0), Vector3.new(1.5, 1, 1))
createWeld(fakeRightLeg, rightLeg, fakeRightLeg)
end
-- RIGHT ARM --
if char.Torso:FindFirstChild("Right Shoulder") and rightArm then
char.Torso:WaitForChild("Right Shoulder").Part0 = nil
local glue = createGlue(torso, rightArm, "Right shoulder")
glue.C0 = CFrame.new(1.5, 0.5, 0, 0, 0, 1, 0, 1, 0, -1, 0, 0)
glue.C1 = CFrame.new(0, 0.5, 0, 0, 0, 1, 0, 1, 0, -1, 0, 0)
local fakeRightArm = createLimb(rightArm, Vector3.new(0,999,0), Vector3.new(1.5,1,1))
createWeld(fakeRightArm, rightArm, fakeRightArm)
end
-- LEFT ARM --
if char.Torso:FindFirstChild("Left Shoulder") and leftArm then
char.Torso:WaitForChild("Left Shoulder").Part0 = nil
local glue = createGlue(torso, leftArm, "Left shoulder")
glue.C0 = CFrame.new(-1.5, 0.5, 0, 0, 0, -1, 0, 1, 0, 1, 0, 0)
glue.C1 = CFrame.new(0, 0.5, 0, 0, 0, -1, 0, 1, 0, 1, 0, 0)
local fakeLeftArm = createLimb(leftArm, Vector3.new(0,999,0), Vector3.new(1.5, 1, 1))
createWeld(fakeLeftArm, leftArm, fakeLeftArm)
end
end)
local resetChar = coroutine.wrap(function(char)
events:WaitForChild("RagdollCameraOff"):FireClient(player)
char.Humanoid.JumpPower = 50
ragdolling = false
wait()
for _, this in pairs(joints) do
--this.Parent = char.Torso -- Add the joints back
this[1].Part0 = this[2]
end
for _, this in pairs(welds) do
this:Destroy() -- destroy old welds
end
for _, this in pairs(fakeLimbs) do
this:Destroy() -- destroy temp parts
end
char.Humanoid.PlatformStand = false
char.Head.CFrame = CFrame.new(char.HumanoidRootPart.Position) * CFrame.new(0,1.5,0)
end)
local Cooldown = coroutine.wrap(function()
wait(.2)
cool = false
end)
if cool == false then
cool = true
if newVal == true and ragdolling == false then
ragdollChar(char)
elseif newVal == false and ragdolling == true then
resetChar(char)
end
Cooldown()
end
end)
end)
end)
What other function or thing i can use to avoid this glitch?

2 comments
Is this in a local script or a regular script
it is server script