So I am trying to make a humanoid ragdoll when it is knocked unconscious (hit in the head by a fast moving object)
But sometimes when I try to set it into a state type it either
- Doesn’t go into the state
- Fakes being in the state (says it’s in the state but doesn’t act like it)
Here’s my current script:
local faces = game.ReplicatedStorage.Faces
local facemgr = require(script.Parent.FaceManager)
local curpained = nil
local paintimer = 0
local cursound = nil
local ragdollscreamed = false
local blushtimer = 5
local neutralphase = 1
local neutraltimer = 0
local consciousness = 1
function neutral(delta)
facemgr:ClearFaces()
neutraltimer -= delta
if (neutraltimer <= 0) then
if (neutralphase == 1) then
neutralphase = 3
neutraltimer = 0.2
elseif (neutralphase == 2) then
neutralphase = 1
neutraltimer = Random.new():NextNumber(3,5)
elseif (neutralphase == 3) then
neutralphase = Random.new():NextInteger(4,5)
neutraltimer = Random.new():NextNumber(3,5)
else
neutralphase = 2
neutraltimer = 0.2
end
end
if (neutralphase == 1) then
facemgr:AddFace(faces.Brows.Neutral)
facemgr:AddFace(faces.Eyes.Normal.Normal.Normal)
facemgr:AddFace(faces.Mouths.Extra.Normal)
elseif (neutralphase == 2 or neutralphase == 3) then
facemgr:AddFace(faces.Brows.Neutral)
facemgr:AddFace(faces.Eyes.Extra.Misc.Closed)
facemgr:AddFace(faces.Mouths.Extra.Normal)
elseif (neutralphase == 4) then
facemgr:AddFace(faces.Brows.Neutral)
facemgr:AddFace(faces.Eyes.Normal.Normal.Look_Left)
facemgr:AddFace(faces.Mouths.Extra.Normal)
elseif (neutralphase == 5) then
facemgr:AddFace(faces.Brows.Neutral)
facemgr:AddFace(faces.Eyes.Normal.Normal.Look_Right)
facemgr:AddFace(faces.Mouths.Extra.Normal)
end
local blushed = false
for i,v in pairs(game.Workspace:GetPartBoundsInRadius(script.Parent.HumanoidRootPart.Position,3)) do
local hum = v.Parent:FindFirstChildOfClass('Humanoid')
if (hum) then
if (v == hum.RootPart) then
if (game.Players:GetPlayerFromCharacter(v.Parent)) then
-- Is player
local facedir = script.Parent.HumanoidRootPart.CFrame.LookVector
local toplr = (v.Position - script.Parent.HumanoidRootPart.Position).Unit
local dotted = facedir:Dot(toplr)
if (dotted >= 0.5) then
-- Player is in front
local plrface = v.CFrame.LookVector
local dotted = facedir:Dot(plrface)
if (dotted <= -0.8) then
-- Player is looking at us
blushtimer -= delta
blushed = true
end
end
end
end
end
end
if (not blushed) then
blushtimer = 5
end
if (blushtimer <= 0) then
facemgr:AddFace(faces.Effects.Soft_Blush)
end
end
function pained()
facemgr:ClearFaces()
local pained_faces = {
faces.Random.Shocked_Stare,
faces.Random.Void_Stare,
faces.Random.Thousand_Yard_Stare,
faces.Random.Tired_Thousand_Yard_Stare
}
if (not curpained) then
curpained = pained_faces[Random.new():NextInteger(1,#pained_faces)]
end
facemgr:AddFace(curpained)
end
function dead()
facemgr:ClearFaces()
facemgr:AddFace(faces.Random.Dead)
end
function hurt()
facemgr:ClearFaces()
facemgr:AddFace(faces.Random.Painted_Beaten_Up)
end
function hurt_bad()
facemgr:ClearFaces()
facemgr:AddFace(faces.Random.Beaten_Up)
end
function ragdoll()
facemgr:ClearFaces()
facemgr:AddFace(faces.Random.Screaming)
local function scream()
if (cursound) then
cursound:Destroy()
end
cursound = game.ReplicatedStorage.FallingScreams:GetChildren()[Random.new():NextInteger(1,#game.ReplicatedStorage.FallingScreams:GetChildren())]:Clone()
cursound.Parent = script.Parent.Head
cursound:Play()
end
if (not ragdollscreamed) then
ragdollscreamed = true
if (not cursound) then
scream()
else
if (not cursound.isPlaying) then
scream()
end
end
end
end
local prevhp = script.Parent.Humanoid.Health
script.Parent.Head.Touched:Connect(function(hit)
local diff = (hit.AssemblyLinearVelocity - script.Parent.Head.AssemblyLinearVelocity).Magnitude
local impact = diff - 10
if (impact > 0) then
consciousness -= impact
end
end)
while true do
local delta = task.wait()
consciousness += delta * 0.1
consciousness = math.clamp(consciousness,0,1)
print(consciousness)
local virusfaces = (#script.VirusFaces:GetChildren() > 0)
if (script.Parent.Humanoid.Health < prevhp) then
paintimer = 3
local function ouch()
if (cursound) then
cursound:Destroy()
end
cursound = game.ReplicatedStorage.Ouch:Clone()
cursound.PitchShiftSoundEffect.Octave = Random.new():NextNumber(0.5,2)
cursound.Parent = script.Parent.Head
cursound:Play()
end
if (script.Parent.Humanoid.Health > 0) then
ouch()
end
end
prevhp = script.Parent.Humanoid.Health
if (script.Parent.Humanoid:GetState() == Enum.HumanoidStateType.Dead) then
dead()
ragdollscreamed = false
else
paintimer -= delta
if (paintimer > 0) then
if (not virusfaces) then
pained()
end
ragdollscreamed = false
else
curpained = nil
if ((script.Parent.Humanoid:GetState() == Enum.HumanoidStateType.FallingDown or script.Parent.Humanoid:GetState() == Enum.HumanoidStateType.Ragdoll) and consciousness > 0.5) then
if (not virusfaces) then
ragdoll()
end
else
ragdollscreamed = false
if (script.Parent.Humanoid.Health <= script.Parent.Humanoid.MaxHealth/4) then
if (not virusfaces) then
hurt_bad()
end
else
if (script.Parent.Humanoid.Health <= script.Parent.Humanoid.MaxHealth/2) then
if (not virusfaces) then
hurt()
end
else
if (not virusfaces) then
neutral(delta)
end
end
end
end
end
end
if (consciousness <= 0.5) then
while (script.Parent.Humanoid:GetState() ~= Enum.HumanoidStateType.FallingDown) do
task.wait()
script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.FallingDown)
print('Attempt')
end
print(script.Parent.Humanoid:GetState())
end
if (virusfaces) then
facemgr:ClearFaces()
for i,v in pairs(script.VirusFaces:GetChildren()) do
facemgr:AddFace(v)
end
end
end
Relevant part:
local consciousness = 1
while true do
local delta = task.wait()
consciousness += delta * 0.1
consciousness = math.clamp(consciousness,0,1)
print(consciousness)
if (consciousness <= 0.5) then
while (script.Parent.Humanoid:GetState() ~= Enum.HumanoidStateType.FallingDown) do
task.wait()
script.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.FallingDown)
print('Attempt')
end
print(script.Parent.Humanoid:GetState())
end
end