Slope Tilt Causes Flickering For Mobile Players

Hello, I currently have a slope tilt script that works pretty well, but in some cases the script causes mobile players’ character models to start flickering!

I’ve seen people experiencing this bug for a while now in my game, and I’m happy that I found the root cause of it!

Here is the script:

local Player = game:GetService("Players").LocalPlayer
repeat task.wait() until Player.Character
local Character = Player.Character
local Humanoid = Character.Humanoid
local rootPart = Character:WaitForChild("HumanoidRootPart")
local Standing
local xzGyro = Instance.new("BodyGyro")
local yGyro = Instance.new("BodyGyro")
yGyro.MaxTorque = Vector3.new(0,3e5,0)
yGyro.P = 5e5
xzGyro.MaxTorque = Vector3.new(3e5,0,3e5)
xzGyro.P = 5e5
xzGyro.Parent = rootPart

local TweenService = game:GetService("TweenService")
local Tweeninfo = TweenInfo.new(
	0.15, -- Tween Length
	Enum.EasingStyle.Linear, -- Style
	Enum.EasingDirection.Out, -- Direction
	0, -- How many times it repeats Tween
	false, -- Reverse tween after complete
	0 -- Should it have a delay	
)

local Tweeninfo2 = TweenInfo.new(
	0.05, -- Tween Length
	Enum.EasingStyle.Linear, -- Style
	Enum.EasingDirection.Out, -- Direction
	0, -- How many times it repeats Tween
	false, -- Reverse tween after complete
	0 -- Should it have a delay	
)

local Tweeninfo3 = TweenInfo.new(
	0.2, -- Tween Length
	Enum.EasingStyle.Linear, -- Style
	Enum.EasingDirection.Out, -- Direction
	0, -- How many times it repeats Tween
	false, -- Reverse tween after complete
	0 -- Should it have a delay	
)

local function getAngle(normal)
	return math.deg(math.acos(normal:Dot(Vector3.yAxis)))
end

while true do task.wait()
	local State = Humanoid:GetState()
	if (State ~=Enum.HumanoidStateType.Freefall)
	and (State ~=Enum.HumanoidStateType.Jumping) then
		Standing = true
	else
		Standing = false
	end
	
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {Character}
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.RespectCanCollide = true
	params.IgnoreWater = true
	params.CollisionGroup = "Default"
	
	local result = workspace:Raycast(rootPart.Position, Vector3.new(0,-10,0), params)

	if result then
		if Standing then
			local currentRightVector = rootPart.CFrame.RightVector
			local upVector = result.Normal
			TweenService:Create(xzGyro,Tweeninfo,{CFrame = CFrame.fromMatrix(rootPart.Position, currentRightVector, upVector)}):Play()
			TweenService:Create(yGyro,Tweeninfo,{CFrame = CFrame.new(rootPart.Position, rootPart.Position)}):Play()
			TweenService:Create(yGyro,Tweeninfo,{MaxTorque = Vector3.new(0,3e5,0)}):Play()
			TweenService:Create(yGyro,Tweeninfo,{P = 5e5}):Play()
			TweenService:Create(xzGyro,Tweeninfo,{MaxTorque = Vector3.new(3e5,0,3e5)}):Play()
			TweenService:Create(xzGyro,Tweeninfo,{P = 5e5}):Play()
			if rootPart.Velocity.Y > 10 then
				if getAngle(result.Normal) >= 15 and getAngle(result.Normal) < 30 then
					TweenService:Create(Humanoid,Tweeninfo2,{HipHeight = 2 + (0.75*1)}):Play()
				elseif getAngle(result.Normal) >= 30 and getAngle(result.Normal) < 45 then
					TweenService:Create(Humanoid,Tweeninfo2,{HipHeight = 2 + (0.75*2)}):Play()
				elseif getAngle(result.Normal) >= 45 and getAngle(result.Normal) < 60 then
					TweenService:Create(Humanoid,Tweeninfo2,{HipHeight = 2 + (0.75*3)}):Play()
				elseif getAngle(result.Normal) >= 60 and getAngle(result.Normal) < 75 then
					TweenService:Create(Humanoid,Tweeninfo2,{HipHeight = 2 + (0.75*4)}):Play()
				elseif getAngle(result.Normal) >= 75 then
					TweenService:Create(Humanoid,Tweeninfo2,{HipHeight = 2 + (0.75*5)}):Play()
				else
					TweenService:Create(Humanoid,Tweeninfo2,{HipHeight = 2}):Play()
				end
			else
				TweenService:Create(Humanoid,Tweeninfo2,{HipHeight = 2}):Play()
			end
		else
			TweenService:Create(xzGyro,Tweeninfo3,{CFrame = CFrame.new(0,0,0)}):Play()
			TweenService:Create(yGyro,Tweeninfo3,{CFrame = CFrame.new(0,0,0)}):Play()
			TweenService:Create(Humanoid,Tweeninfo2,{HipHeight = 2}):Play()
		end
	else
		TweenService:Create(xzGyro,Tweeninfo3,{CFrame = CFrame.new(0,0,0)}):Play()
		TweenService:Create(yGyro,Tweeninfo3,{CFrame = CFrame.new(0,0,0)}):Play()
		TweenService:Create(Humanoid,Tweeninfo2,{HipHeight = 2}):Play()
	end
end

Here is a clip of the glitch in action that my friend took!

It’d be a lifesaver if you could see if anything is wrong with my script, thank you for helping!

1 Like

I found out that the tween parts of the code cause the bug, but how can I provide an alternative for them?