So I made a script that prints out how much time the player had been free falling. but I want the script to detect if the Airtime has been more than 0.5 seconds so a wind sound can start playing. But I don’t know how to make it detect if more than 0.5 seconds have passed while falling. This is the script:
local Humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
local StartTime
Humanoid.StateChanged:Connect(function(_, State)
if State == Enum.HumanoidStateType.Freefall then
StartTime = tick()
elseif State == Enum.HumanoidStateType.Landed then
local AirTime = tick() - StartTime
print("Was in air for "..AirTime.."s")
end
end)
local Humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
local StartTime
Humanoid.StateChanged:Connect(function(_, State)
if State == Enum.HumanoidStateType.Freefall then
StartTime = tick()
elseif State == Enum.HumanoidStateType.Landed then
local AirTime = tick() - StartTime
print("Was in air for "..AirTime.."s")
else if State == Enum.HumanoidStateType.Freefall then
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://6960193959"
if AirTime == 0.5 then
sound:Play()
end
end
end
end)
You are not waiting .5 seconds the script ends quicker than when you check if its .5 seconds. You should use wait() or delay() i’m not sure which is better in this case.
local Humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://6960193959"
sound.Volume = 3
local StartTime
Humanoid.StateChanged:Connect(function(_, State)
if State == Enum.HumanoidStateType.Freefall then
StartTime = tick()
local AirTime = tick() - StartTime
if AirTime >= 0.5 then
sound:Play()
end
elseif State == Enum.HumanoidStateType.Landed then
local AirTime = tick() - StartTime
print("Was in air for "..AirTime.."s")
sound:Stop()
end
end)
but it doesn’t play the sound. even if I’ve been falling for more than 0.5 seconds
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = char:WaitForChild("Humanoid")
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://6960193959"
sound.Volume = 3
local StartTime
Humanoid.StateChanged:Connect(function(OldState,NewState)
if NewState == Enum.HumanoidStateType.Freefall then
StartTime = tick()
delay(.5, function()
if tick() - StartTime >= .5 and Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
sound:Play()
end
end)
elseif Humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then
sound:Stop()
end
end)
-- local script
local ps = game:GetService("Players")
local lp = ps.LocalPlayer
local TimeInAir = 0
while wait() do
local ch = lp.Character
if ch then
local c = RaycastParams.new()
c.FilterType = Enum.RaycastFilterType.Blacklist
c.FilterDescendantsInstances({ch})
local r = workspace:Raycast(ch.HumanoidRootPart.Position,Vector3.new(0,-10,0),c)
if not r.Instance then
TimeInAir = TimeInAir+1
-- There is nothing underneath, assume in air.
else
TimeInAir = 0
-- Not in air.
end
end
local Humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
local lastFall = 0
local sound = Instance.new("Sound", game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart"))
sound.SoundId = "rbxassetid://6960193959"
sound.Volume = 3
Humanoid.StateChanged:Connect(function(_, State)
if State == Enum.HumanoidStateType.Freefall then
lastFall += 1
local myLast = lastFall
delay(0.5, function()
if myLast == lastFall then
sound:Play()
end
end)
elseif State == Enum.HumanoidStateType.Landed then
sound:Stop()
end
end)
If you want to be fancy and want the sound to fade in
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local Humanoid = char:WaitForChild("Humanoid")
local sound = Instance.new("Sound", game.Workspace)
sound.SoundId = "rbxassetid://6960193959"
local Volume = 3
local StartTime
Humanoid.StateChanged:Connect(function(OldState,NewState)
if NewState == Enum.HumanoidStateType.Freefall then
StartTime = tick()
delay(.5, function()
if tick() - StartTime >= .5 and Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
sound.Volume = 0
sound:Play()
for count = 1,10 do
local Incr = Volume/10
sound.Volume = sound.Volume + Incr
wait(0.1)
end
end
end)
elseif Humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then
sound:Stop()
end
end)