okay so i made this ragdoll script that ragdolls when the player is in the air for more than 0.7 seconds.
this only works before the player dies, if the player does die it doesnt work again. i tried to fix it in the script but that didnt work so i need someones help! if you can repair the code please reply!
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local deadly = false
local fallingEvent = game:GetService("ReplicatedStorage"):WaitForChild("FallingEvent")
local fallingStartTime = nil
local function onCharacterAdded()
print("Player respawned")
fallingStartTime = nil
print("reset fallingtime")
print(fallingStartTime)
end
player.CharacterAdded:Connect(onCharacterAdded)
humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Freefall then
fallingStartTime = tick()
elseif oldState == Enum.HumanoidStateType.Freefall then
local fallingDuration = tick() - fallingStartTime
if fallingDuration > 0.7 then
fallingEvent:FireServer()
end
fallingStartTime = nil
end
end)
Put all this code as well as the character and humanoid variables inside the onCharacterAdded function. StateChanged is being connected for the first time but when the player dies, it is disconnected and is not connected back since the player receives a new character and humanoid instance.
local player = game.Players.LocalPlayer
local deadly = false
local fallingEvent = game:GetService("ReplicatedStorage"):WaitForChild("FallingEvent")
local fallingStartTime = nil
local function onCharacterAdded()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
fallingStartTime = nil
humanoid.StateChanged:Connect(function(oldState, newState)
if newState == Enum.HumanoidStateType.Freefall then
fallingStartTime = tick()
elseif oldState == Enum.HumanoidStateType.Freefall then
local fallingDuration = tick() - fallingStartTime
if fallingDuration > 0.7 then
fallingEvent:FireServer()
end
fallingStartTime = nil
end
end)
print("Player respawned")
print("reset fallingtime")
print(fallingStartTime)
end
player.CharacterAdded:Connect(onCharacterAdded)