I’m creating a rhythm game, and I want the buttons/arrows to appear on a SurfaceGUI and move upwards, where I can then detect whether the player hits them or not. However, I’m having a problem. I’ve tried to move them upwards by changing the Udim2’s offset by a set amount, but it won’t move!
I have a Script (not local, i want everyone to see it) parented to the label, containing this.
local label = script.Parent
local runservice = game:GetService("RunService")
runservice:BindToRenderStep("Test", 1, function(delta)
label.Position = UDim2.fromOffset(label.Position.X.Offset, label.Position.Y.Offset - 7.14285714286 * delta)
end)
No, I don’t want to use a Tween. I want to move it by an exact, constant amount every second. The script isn’t even changing the Position at all, what do I do?
I see you have the right idea but not the right math. I’m assuming you want to move the frame ~7 pixels per second, and in that case, you can get the time elapsed(s) and get that time and multiply it by the offset:
local label = script.Parent
local T1 = os.clock()
while RS.RenderStpped:Wait() do -- Using a true do loop because it's simpler
label.Position = UDim2.fromOffset(label.Position.X.Offset, label.Position.Y.Offset - 7.14285714286 * (os.clock() - T1))
end
--[[
Basically, 7px/second, so if 2 seconds
passed, we multiply 7 by 2 becuase 2
seconds has passed(you get 14). If 1.5
seconds pass then you would theoretically
go 10.5 pixels, and using the formula you
get 10.5 [7 * d = 7 * 1.5 = 10.5)
]]
For some reason this won’t work. I put it in a LocalScript since RenderStepped only runs in one, and also switched it from being parented to the Adornee to being in StarterGUI but it still won’t work. (Also, RenderStpped.)
There are no errors in the output.
EDIT: After some working, found out that it was just going too fast for me to see, and Roblox wasn’t changing the position in the editor.
My bad, it seems like it would go to fast(as stated in PMs). The error in the code I provided is that i didn’t consider changing the T1 variable. Instead, I’ll get the original position of the frame then offset based off of that:
local label = script.Parent
local T1 = os.clock()
local OriginalPos = label.Position
while RS.RenderStpped:Wait() do
label.Position = OriginalPos - UDim2.fromOffset(0, 7.14285714286 * (os.clock() - T1))
--[[
Offsetting based off of time and initial value
]]
end