Greetings!
I only recently got into scripting. Looking up the DevForum was a massive help to my learning (thank you for everything!) and I figured I could seek some help directly.
So basically, in my game, the player is supposed to obtain an ability that allows them to slow their fall when they hold shift while being mid-air. It works! Whenever the player is mid-air, their fall is slowed for as long as the Shift key is held and everything goes back to normal if they release it.
(The game is going to be a metroidvania so I want to implement abilities that help with moving around on the map. I figured unlockable abilities that require key press would be best.)
However, I noticed one issue that isn’t quite desired:
As the title of this topic says, the slow fall is activated even if I’m not in the process of falling!
Whenever I hold the shift key while on the ground, the player sinks into it.
The slow fall itself is already how I want it to be (the player falls at a fixed and constant speed, just like those parachutes from old Roblox games), but I want to make it so if the player is not in freefall, it can’t be activated.
Here’s the whole script! I added some comments too.
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local A = require(game.ReplicatedStorage:WaitForChild("AbilityModule"))
local pGui = A.player:WaitForChild("PlayerGui")
local sfGui = pGui:WaitForChild("AbilityGui").SlowFall
local sfGiverEvent = ReplicatedStorage:WaitForChild("sfGiverEvent")
sfGui.Text = "Slow Fall Disabled"
--Everything above this comment works perfectly and may be irrelevant
local function onCharacterAdded(character)
local humanoid = character:WaitForChild("Humanoid")
local bVelo = Instance.new("BodyVelocity", A.player.Character:WaitForChild("HumanoidRootPart"))
bVelo.MaxForce = Vector3.new(0,1,0)
if A.SlowFon then --Slow fall can only be activated if the player obtained it. No issue here.
humanoid.StateChanged:Connect(function(old, new)
if new == Enum.HumanoidStateType.Freefall then --Supposed to proceed only if player is in freefall,
UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.LeftShift then --Activates slow fall, works well
bVelo.MaxForce = Vector3.new(0,100000,0)
bVelo.P = 10000
bVelo.Velocity = Vector3.new(0,-10,0)
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.KeyCode==Enum.KeyCode.LeftShift then --Stops slow fall, works well
bVelo.MaxForce = Vector3.new(0,1,0)
bVelo.Velocity = Vector3.new(0,16,0)
bVelo.P = 0
end
end)
else --I tried doing "elseif new ~= Enum.HumanoidStateType.Freefall then" but it didn't seem to make a difference.
end
end)
end
end
local function onGiverFired()
A.SlowFon = true
sfGui.Text = "Slow Fall Enabled!"
onCharacterAdded(A.player.Character)
end
sfGiverEvent.OnClientEvent:Connect(onGiverFired)
A.player.CharacterAdded:connect(onCharacterAdded)
Note that may be important
I played around with the issue some more after writing this post and it appears that when the player just got the ability or respawned while having it, the issue doesn’t happen.
The issue only starts happening after the player jumped for the first time after spawning/getting the ability.
In other words, whenever onCharacterAdded is called, the issue isn’t here until the player jumps or falls for the first time and, of course, unlocked the ability.
I thank you in advance for your help! I have a feeling I wasn’t too far from the solution.
I hope I was descriptive enough!