Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Problem with calculating target position for a prismatic constraint based elevator?

Asked by
4jnx 50
3 years ago
Edited 3 years ago

Hello, So I'm having a problem with my prismatic constraint elevator.

When I click the button for a floor it will calculate the target position for the prismatic constraint by firing a function that has a parameter called "obj" this obj parameter is the floor part I will be sending through to the function it then calculates the correct target position for the prismatic constraint by subtracting the prismatic constraints target position from the elevator platforms Y position then adding the obj parameters Y position (the elevator platform is the part that has the prismatic constraint in it, basically the main part)

This works perfectly when I'm already at a floor or the elevator has stopped BUT when I am already going down or up to a platform and click a button while in motion it misses the floor i want to go to but when it has stopped at the wrong position and i then press the button for the floor i missed again it then goes there perfectly fine...

I am very confused.

Code snippet for calculating target position:

function calcstud(obj)
  local stud =  ElevatorPlatform.Position.Y - ElevatorPlatform.PrismaticConstraint.TargetPosition+ 
  obj.Position.Y

  return stud
end

Code snippet for getting input from a button:

for i, child in ipairs(ButtonsGridGui:GetChildren()) do
  if child:IsA("TextButton") then
    child.MouseButton1Click:Connect(function()
      print("Button "..child.Name.." was clicked")
      local result = calcstud(Floors:FindFirstChild(child.Name))
      ElevatorPlatform.PrismaticConstraint.TargetPosition = result
      print(result)
    end)
  end
end

Some details about some of the stuff referenced in the code:

There is a group for all the floors each floor is just a part that the elevator goes up to

The elevator can have inf floors meaning I'm using a surface GUI with a scrolling frame and a UIGridLayout for buttons (there is a function for setting up buttons depending on amount of floors but that has nothing to do with the problem)

The buttons for each floor and the floors are numbered from 1 to whatever amount of floors you have, this means i can detect what button was pressed and find the floor by just searching in floor folder for the buttons name

Video of what's happening: https://streamable.com/2oo8za

Hopefully I can get some answers!

0
https://www.roblox.com/games/7178302913/Elevator? If you wanna test the elevator 4jnx 50 — 3y
0
No idea what the problem is, just make it go to the position of whatever the selected floor is, yeah? AlexanderYar 788 — 3y
0
i can do that BUT if the y position of the elevator isn't 0 then it will miss the floor as the target pos is still 0 no matter what the y pos of the elevator platform 4jnx 50 — 3y

1 answer

Log in to vote
0
Answered by
4jnx 50
3 years ago

Ok!

I've fixed it

I'm not exactly sure why my above code wasn't working but what I've done now is I've gotten the bottom floor in the floors group (floor 1) subtracting it by the floor your goings to's Y pos then got the magnitude giving me the amount of studs between them

Code for that:

function calcstud(obj2)
  local stud = (Floors:FindFirstChild(1).Position - obj2.Position).Magnitude

  return stud
end

Hope this helps any one else having a problem like this.

Ad

Answer this question