Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Script isnt detecting the correct position of UI?

When updating the position of a frame, tweening or not, the script checks the specific Y Scale value on the Frame to see if it doesn't equal the value it gets set to

The script initially is supposed to ignore the if statement, and go to the else statement meaning since the frame hasn't been tweened yet, and once another tween is called, the if statement clears the tween basically

For Example:

message = values.Message.Changed:Connect(function()
			if (values.Message.Value) then
				UI.MessageFrame.Visible = true;
				if (UI.MessageFrame.Position.Y.Scale ~= .9) then
					if (UI.MessageFrame.Position.Y.Scale > .79) then
						local start = tick();
						repeat wait() until (UI.MessageFrame.Position.Y.Scale <= .82 or tick()-start >= .5)
						task.wait(1)
					end
					tweenObj(UI.MessageFrame, 1, {Position = UDim2.new(0.822, 0, 0.9, 0)});
					task.wait(1.3)
					if (values.Message.Value) then
						UI.MessageFrame.Message.Text = values.Message.Value;
						tweenObj(UI.MessageFrame, 1, {Position = UDim2.new(0.822, 0, 0.79, 0)});
						wait(2.5)
						tweenObj(UI.MessageFrame, 1, {Position = UDim2.new(0.822, 0, 0.9, 0)});
						wait(1)
						UI.MessageFrame.Visible = false;
					end
				else
					warn"ya"
					UI.MessageFrame.Message.Text = values.Message.Value;
					tweenObj(UI.MessageFrame, 1, {Position = UDim2.new(0.822, 0, 0.79, 0)});
					task.wait(2.5)
					tweenObj(UI.MessageFrame, 1, {Position = UDim2.new(0.822, 0, 0.9, 0)});
					wait(1)
					UI.MessageFrame.Visible = false;
				end
			else
				if (UI.MessageFrame.Position.Y.Scale ~= .9) then
					tweenObj(UI.MessageFrame, 1, {Position = UDim2.new(0.822, 0, 0.9, 0)});
					wait(1)
					UI.MessageFrame.Visible = false;
				end
			end
		end)

Segment that isn't running properly:

if (UI.MessageFrame.Position.Y.Scale ~= .9) then

At this portion, when I print the Y Position, it prints 0.89999999764121, instead of 0.9, as in the position property of the frame at the exact moment says {0.822, 0},{0.9, 0}

Does anyone have any related issues to this? I'm only posting this because it has always worked before, and now all of a sudden it starts with this issue. I feel as if this is a ROBLOX API issue at the moment.

1 answer

It's not about ROBLOX api, it's floating point issues as every other programming language also does have a problem about it. I'm not here to say why does it cause the issue as you can just search up "floating point issue" on Google and there will be plenty of explanation about it. Anyways, since ROBLOX caps their floating point on the third floating digit, you can do:

local function Round(number, increment)
	return math.round(number * (1 / increment)) / (1 / increment)
end

if Round(UI.MessageFrame.Position.Y.Scale, 1 / 10 ^ 3) ~= .9) then

Not sure if this works, but if it can solve your problem please mark this answer as the solution

This discussion is preserved as it was. New answers, comments, and votes are closed.