I am having some issues getting this character selection’ visuals to fully work. It’s mostly there but there are some visual glitches when spam dragging/using input keys (A, D, left and right arrows).
I’m thinking a boolean debounce may fix the issues. But I want to save that approach for if nothing else works, as I feel that may hinder on the UIs responsiveness.
Here is the LocalScript. I don’t think its missing anything but if it is i can send the full script as this is the section that is dedicated just to the character selection (i think).
local selectedIndex = 1
local spinSpeed = 35 -- degrees per second
local swipeThreshold = 50 -- pixels for swipe detection
--adjust spacing and size based on device
local viewportSizeX = 0.25
local viewportSpacing = if playerModule.getPlayerDevice() == "Mobile" then 120 else 300
local characterShopTweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Quad)
local viewportFrames = {}
local cancelPauseCountdown = false
local muteEnabled = false
local shadowsEnabled = true
local wheelDebounce = false
local wheelDebounceTime = 0.12
local dragTween = nil
local isDragging = false
local dragStartPos = 0
local dragDelta = 0
function getViewportYSize(selected)
if playerModule.getPlayerDevice() == "Mobile" then return selected and 0.3 or 0.15 end --0.3 selected size, 0.15 not selected size
return selected and 0.5 or 0.25 --0.5 selected size, 0.25 not selected size
end
--create character shop frames
function createViewport(characterData, positionIndex)
local vp = characterTemplate:Clone()
vp.Visible = true
vp.Name = characterData.Name
vp.Size = UDim2.new(viewportSizeX, 0, getViewportYSize(false), 0)
vp.Position = UDim2.new(0.5, (positionIndex - 1) * viewportSpacing, 0.5, 0)
vp.AnchorPoint = Vector2.new(0.5, 0.5)
vp.Parent = charactersContainer
local charClone = characterData.Model:Clone()
charClone.CFrame = CFrame.new(0, 5.113, 0) * CFrame.Angles(0, math.rad(-180), 0)
charClone:SetAttribute("BaseCFrame", charClone.CFrame)
charClone.Name = "Character"
charClone.Parent = vp
return vp
end
for i, characterData in ipairs(characters.Characters) do
local vp = createViewport(characterData, i)
table.insert(viewportFrames, vp)
end
--character shop frame positioning/sizing
function centerCharacter(index)
for i, vp in ipairs(viewportFrames) do
local targetX = (i - index) * viewportSpacing
local selected = if (i == index) then true else false
TweenService:Create(vp, characterShopTweenInfo, {
Position = UDim2.new(0.5, targetX, 0.5, 0),
Size = UDim2.new(viewportSizeX, 0, getViewportYSize(selected), 0)
}):Play()
if not vp.Character or i == index then continue end
local baseCFrame = vp.Character:GetAttribute("BaseCFrame")
if not baseCFrame then continue end
TweenService:Create(vp.Character, characterShopTweenInfo, {CFrame = baseCFrame}):Play()
end
end
function selectCharacter(index)
selectedIndex = math.clamp(index, 1, #viewportFrames)
centerCharacter(selectedIndex)
end
--update positions and sizes in real time
function updateViewportPositions()
if #viewportFrames == 0 then return end
--screen center in pixels (world viewport size)
local screenCenterX = (camera and camera.ViewportSize and camera.ViewportSize.X or 0) / 2
local minY = getViewportYSize(false)
local maxY = getViewportYSize(true)
for i, vp in ipairs(viewportFrames) do
--x offset includes the dragDelta for dragging
local dragOffset = dragDelta / viewportSpacing
local targetX = (i - selectedIndex + dragOffset) * viewportSpacing
vp.Position = UDim2.new(0.5, targetX, 0.5, 0) --position update for realtime response
--compute distance from viewport center (use absolutePosition if available)
local centerX = (vp.AbsolutePosition and vp.AbsoluteSize and vp.AbsoluteSize.X>0) and
vp.AbsolutePosition.X + vp.AbsoluteSize.X/2 or screenCenterX + targetX
local distance = math.abs(centerX - screenCenterX)
local t = math.clamp(1 - (distance / viewportSpacing), 0, 1)
local easeT = t * t * (3 - 2 * t)
vp.Size = UDim2.new(viewportSizeX, 0, minY + (maxY - minY) * easeT, 0)
end
end
function tweenDragToCenter(nearestIndex)
if dragTween then
dragTween:Cancel()
end
local goalDelta = (selectedIndex - nearestIndex) * viewportSpacing
local proxy = Instance.new("NumberValue")
proxy.Value = dragDelta
proxy.Changed:Connect(function(new)
dragDelta = proxy.Value
updateViewportPositions()
end)
dragTween = TweenService:Create(proxy, characterShopTweenInfo, {Value = goalDelta})
dragTween:Play()
dragTween.Completed:Connect(function()
proxy:Destroy()
dragDelta = 0
dragTween = nil
selectCharacter(nearestIndex)
end)
end
--dragging
function onDragStart(input)
if not shopFrame.Visible then return end
if input.UserInputType ~= Enum.UserInputType.Touch and input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
isDragging = true
dragStartPos = input.Position.X
dragDelta = 0
end
function onDragMove(input)
if not isDragging then return end
if input.UserInputType ~= Enum.UserInputType.Touch and input.UserInputType ~= Enum.UserInputType.MouseMovement then return end
dragDelta = input.Position.X - dragStartPos
updateViewportPositions()
end
function onDragEnd(input)
if not isDragging then return end
if input.UserInputType ~= Enum.UserInputType.Touch and input.UserInputType ~= Enum.UserInputType.MouseButton1 then return end
isDragging = false
--get nearest index after release
local rawNearest = selectedIndex - math.floor((dragDelta + viewportSpacing / 2) / viewportSpacing)
tweenDragToCenter(math.clamp(rawNearest, 1, #viewportFrames))
dragDelta = 0
end
function moveSelectionSmooth(targetIndex)
tweenDragToCenter(math.clamp(targetIndex, 1, #viewportFrames))
end
-- Connect inputs
UserInputService.InputBegan:Connect(onDragStart)
UserInputService.InputChanged:Connect(onDragMove)
UserInputService.InputEnded:Connect(onDragEnd)
UserInputService.TouchStarted:Connect(onDragStart)
UserInputService.TouchMoved:Connect(onDragMove)
UserInputService.TouchEnded:Connect(onDragEnd)
mouse.WheelForward:Connect(function()
if wheelDebounce or not shopFrame.Visible then return end
wheelDebounce = true
delay(wheelDebounceTime, function()
wheelDebounce = false
end)
moveSelectionSmooth(selectedIndex-1)
end)
mouse.WheelBackward:Connect(function()
if wheelDebounce or not shopFrame.Visible then return end
wheelDebounce = true
delay(wheelDebounceTime, function()
wheelDebounce = false
end)
moveSelectionSmooth(selectedIndex+1)
end)
UserInputService.InputBegan:Connect(function(input, gpe)
if not shopFrame.Visible or gpe or input.UserInputType ~= Enum.UserInputType.Keyboard then return end
local keycode = input.KeyCode
if keycode == Enum.KeyCode.A or keycode == Enum.KeyCode.Left then
moveSelectionSmooth(selectedIndex - 1)
elseif keycode == Enum.KeyCode.D or keycode == Enum.KeyCode.Right then
moveSelectionSmooth(selectedIndex + 1)
end
end)
--spinning for selected character and realtime ui when dragging character shop frames
RunService.RenderStepped:Connect(function(dt)
if not shopFrame.Visible then return end
local vp = viewportFrames[selectedIndex]
if vp and vp.Character then
vp.Character.CFrame *= CFrame.Angles(0, math.rad(spinSpeed * dt), 0)
end
--keep updating viewport sizes/positions so enlargement is realtime when dragging
updateViewportPositions()
end)