Script UDim2 Error

When I run my code this error shows up: TweenService:Create property named ‘Position’ cannot be tweened due to type mismatch (property is a ‘UDim2’, but given type is ‘double’)

I don’t really know what it means.

My script has two problems first one is that every thing newTemplate is the same and the second one is the error wich I marked in the script.

When getting a random character from the module you will get 2 values the first one is the character name and the second one is the chance percentage:

local tweenService = game:GetService("TweenService")
local sendAnimation = game.ReplicatedStorage.RemoteEvents.SendAnimation
local characterModule = require(game.ReplicatedStorage.CharacterModule)
local template = script.Template
local player = game.Players.LocalPlayer
local itemContainer = script.Parent.ItemContainer
local spinTime = 5

sendAnimation.OnClientEvent:Connect(function(character, chance)
	local numberOfItems = math.random(50,100)
	local chosenPosition = numberOfItems - math.random(5, 10)
	
	for i = 1, numberOfItems do
		local newItem = i == chosenPosition and character or
			characterModule.chooseRandomCharacter(player.Values.Luck.Value)
		
		local newItemFrame = template:Clone()
		print(character)
		newItemFrame.Title.Text = character
		newItemFrame.Chance.Text = chance.."%"
			
		newItemFrame.Parent = itemContainer
	end
	
	local cellSize = template.Size.X.Scale
	local padding = itemContainer.UIListLayout.Padding.Scale
	local nextOffset = -cellSize - padding
	local finalPosition = (0.5 - cellSize / 2) + (chosenPosition - 1) * nextOffset
	
	local spinTweenInfo = TweenInfo.new(spinTime, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
	print(finalPosition, itemContainer.Position)
	local tween = tweenService:Create(itemContainer, spinTweenInfo, {Position = finalPosition})
	tween:Play()
	
	for _, item in pairs(itemContainer:GetChildren()) do
		if item and not item:IsA("UIListLayout") and not item:IsA("UIPadding") then
			item:Destroy()
		end
	end
end)

I got this script from someone elses code, he said he had the same problem but looked at the video and fixed it himself. He never told how. So can someone help me?

2 Likes

UDim2 is a datatype (much like Vector3s or CFrames) which is used for UI. It has 4 values for scale and offset for both X and Y. In this case it seems like you’re trying to tween a UI object, but the finalPosition variable isn’t a UDim2 so what you need to do is to do something like UDim2.new(0, finalPosition, 0, 0) or something in order to make the finalPosition a UDim2 (can’t really tell you exactly what to do since i don’t know how to read code well but i hope you get the idea you need)

1 Like

The finalPosition Variable passed is not a UDim2 value. To fix this, place the variables into a UDim2 format so that the GUI gets tweened correctly

I tried like the other person said, but it will just tween to the corners I tried every number. Do you know why?

1 Like

You should use Udim2s because finalPosition is only a number, example of a Udim2 tween:

local YourTweenInfo --your tweeninfo here
local MainInstance --your instance here
local X = {Scale = 0.1, Offset = 20}
local Y = {Scale = 0.1, Offset = 20}
local Tween = TweenService:Create(MainInstance, YourTweenInfo, {Position = Udim2.new(X.Scale, X.Offset, Y.Scale, Y.Offset)}) 
1 Like

It does tween but it tween to the top left, its supossed to make a spin effect like in the video of that post.

1 Like

This was just an example script you have to modify each X and Y tables.