Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Making 2D friction in a GUI and the player does not slide on surface when not moving, help?

Asked by 6 years ago
Edited 6 years ago

For some reason the deceleration is not working. so when player stops pressing the arrow key he doesn't slide :(

local PlayerVelocity = script.Parent:WaitForChild("PlayerVelocity")
local PlayerMovementValue = script.Parent:WaitForChild("PlayerMovementValue")
local uis = game:GetService("UserInputService")

--Whenever player is moving, MovementValue is equal to 1.
--Whenever player is _not_ moving, MovementValue is equal to 0.

uis.InputBegan:connect(function(input,gp)
   if input.KeyCode == Enum.KeyCode.Left  then
--[[Accelerating:]] 
while PlayerMovementValue.Value == 1 do
    wait()
    if (PlayerVelocity.Value < 0) then

        PlayerVelocity.Value = (PlayerVelocity.Value + 0.5)*PlayerMovementValue.Value

    elseif (PlayerVelocity.Value < 3) then

        PlayerVelocity.Value = (PlayerVelocity.Value + 0.046875)*PlayerMovementValue.Value
else
--[Decelerating]]
 PlayerVelocity.Value = PlayerVelocity.Value -math.min(math.abs(PlayerVelocity.Value),0.046875)
*math.sign(PlayerVelocity.Value)
end
end 
end
end)

Answer this question