The function is working as expected; it returns a number. If you can share the rest of your code, we can identify the problem. It seems that you are trying to pass a number instead of a UDim2 value to some UI element.
I couldn’t find any related element in this script that would cause the error message you mentioned. In the error message you post, there is a element called “SliderChange” but I couldn’t find it in the script. Could it be that this error is not related to this script?
Yeah I think I found the problem. You are trying to access X value of UDim2 but you are not telling which X value. You can access the X value like this:
Dragger.Position.X is a UDim value. The position property of UIs are all UDim2s, and the X and Y components are UDims. Frame.AbsolutePosition.X is a number value. AbsolutePosition is always measured in Vector2, so the X and Y components are numbers.
That’s your problem. You are trying to do math with two completely unrelated datatypes.
I finally figured out the problem with the script and changed a few things about it other than line 39. I will share the final script in case a person stumbles upon this topic:
Working ui slider system:
local Player = game:GetService("Players").LocalPlayer
local mouse = Player:GetMouse()
local setting = script.Parent.Name
local module = require(game.ReplicatedStorage.Modules:WaitForChild("ServerInventory"))
local sound = game.SoundService:WaitForChild("Click2Effect")
local IsDragging = false
function roundDeci(n, decimal)
return math.round(n * 10 ^ decimal) / (10 ^ decimal)
end
local Dragger = script.Parent.Slider.Frame:WaitForChild("SliderIndicator")
local beam = script.Parent.Slider.Frame:WaitForChild("SliderBeam")
local parameters = script:WaitForChild("Parameters")
local min = parameters.Value.X
local default = parameters.Value.Y
local max = parameters.Value.Z
local Frame = Dragger.Parent
local interaction = Frame:WaitForChild("Interaction")
local label = Frame.Parent:WaitForChild("CurrentAmount")
local defaultpercentage = roundDeci(default/max, script:WaitForChild("Round").Value)
Dragger.Position = UDim2.fromScale(defaultpercentage, Dragger.Position.Y.Scale)
label.Text = roundDeci(max*defaultpercentage, script.Round.Value)
interaction.MouseButton1Down:Connect(function()
IsDragging = true
end)
local function buttonlift()
if not IsDragging then return end
IsDragging = false
Player.Settings[setting].Value = roundDeci(Dragger.Position.X.Scale * max, script.Round.Value)
module.settingsevent(Player.Settings[setting], Player.Settings[setting].Value)
print(Dragger.Position.X.Scale)
label.Text = roundDeci(Dragger.Position.X.Scale * max, script.Round.Value)
sound:Stop()
end
mouse.Move:Connect(function()
if IsDragging then
sound.TimePosition = 0
sound:Play()
local newPositionX = math.clamp((mouse.X - Frame.AbsolutePosition.X)/Frame.AbsoluteSize.X, 0, 1)
Dragger.Position = UDim2.fromScale(newPositionX, Dragger.Position.Y.Scale)
label.Text = roundDeci(Dragger.Position.X.Scale * max, script.Round.Value)
end
end)
mouse.Button1Up:Connect(buttonlift)
interaction.MouseButton1Up:Connect(buttonlift)
I will give the solution to @Prototrode for giving me the idea but thank you @GeldiBaskan for helping me with this issue.