I have a script using tweenservice to make this elevator go up and down. I want a surfacegui on a part to accurately reflect which floor it’s on. How can I do something like change the textlabel’s text based on the elevator union’s current position passing a certain height?
Elevator tween:
local Part = script.Parent -- Change this to your part
local TweenService = game:GetService("TweenService")
local Pos = Vector3.new(38.03, 31.5, 144.31) -- Change this to the position you want part to move to
local Moving_Info = TweenInfo.new(
9,-- the amount of seconds you want it to take
Enum.EasingStyle.Quad,
Enum.EasingDirection.InOut,
9999, --The amount of time you want it to repeat
true, --Whether you want it to reverse back to the original position when it's done
0 -- how much time should delay between each repitition
)
TweenService:Create(Part, Moving_Info, {Position = Pos}):Play()
floor_part.Position.Y is the Y position of the floor_part and can be replaced with what you think is its Y position.
if (elevator_part.Position.Y >= 20) then
(And if elevator_part’s position is, say , (50, 8, -7), then it’ll be the same as…
if (8 >= 20) then -- is 8 greater than or equal to 20?
… except elevator_part’s position can change, so you need some code to find where it is currently)
The problems you’ll soon have are, how to detect different floors, and how to make it change by itself (“will it work with tweens?”)
The second one is most easily solved by looping/waiting/checking where the elevator is every 0.1 seconds or so
The first one is solved by using more ifs, or even better, elseifs:
if (elevator_part.Position.Y < 0) then -- note that I replaced >= with < so the floors are checked bottom to top
board.text = "Underground"
elseif (elevator_part.Position.Y < 10) then -- between 0 and 10
board.text = 1
elseif (elevator_part.Position.Y < 20) then -- between 10 and 20
board.text = 2
elseif (elevator_part.Position.Y < 30) then -- between 20 and 30
board.text = 3
else -- elevator_part's Y isnt less than 30, so it must be more than that
board.text = "In the sky"
end
you can just set an intvalue “CurrentFloor”, and then each time it moves, have it take an intvalue from the floor that specifies which floor it is. when it moves from floor to floor, you can have it be 0 or some other value to specify that it’s moving in between. i wouldn’t reccomend floats, as they can be a bit buggy at times, though they should still be able to represent intvalues just fine, so you can also just have it be floor = 4.5 when it’s between 4 and 5.
If you want it using the Yaxis, it will be very unreliable. a value of 49.99 will mess up the scripts mentioned above. so what you can do to make it even more efficient and also be more reliable, is it put a bunch of functions for each floor (if something needs to happen to the floor/elevator when it arrives) inside of an array. then to acces start one of these functions you’ll only need to call an adress from inside that array with math.floor(Part.Position.Y/FloorHeight) (assuming they’re all equal). it will look about like this: Array[math.floor(Part.Position.Y/FloorHeight) ]()
anyways, to get the current floor, math.floor(Part.Position.Y/FloorHeight) is the most important when using the Yaxis.