Hello!
I am trying to recreate a machine we have at my work ( an cnc machine) but the part won’t stay in between the ‘machine park’. I’ve tried using booleans which caused the whole script to malfunction. The machine is powered by a UI with just 2 scripts (1 local script for the buttons and 1 script for the remote events). Does anybody have tips on how I could do this?
video of what I mean:
The script I’ve tried:
--[[ CNC AXLE LIMITS ]]
local MachineHead = game.Workspace.MachineHead
local Main = script.Parent
local AxleLimits = Main.AxleLimits
local MaxXaxle = AxleLimits.MaxXaxle
local MaxYaxle = AxleLimits.MaxYaxle
local MaxZaxle = AxleLimits.MaxZaxle
local MinXaxle = AxleLimits.MinXaxle
local MinYaxle = AxleLimits.MinYaxle
local MinZaxle = AxleLimits.MinZaxle
MachineHead.Changed:Connect(function()
print("Machine head current position: ", MachineHead.Position)
if MachineHead.Position == Vector3.new(6, MachineHead.Position.Y, MachineHead.Position.Z) then
MaxXaxle.Value = true
elseif MachineHead.Position == Vector3.new(MachineHead.Position.X, 2.6, MachineHead.Position.Z) then
MaxYaxle.Value = true
elseif MachineHead.Position == Vector3.new(MachineHead.Position.X, MachineHead.Position.Y, 50) then
MaxZaxle.Value = true
else
MaxXaxle.Value = false
MaxYaxle.Value = false
MaxZaxle.Value = false
MinXaxle.Value = false
MinYaxle.Value = false
MinZaxle.Value = false
end
end)
[ main scripts ]
The button script:
--[[ CNC MACHINE BUTTONS ]]
local UIS = game:GetService("UserInputService")
local Main = script.Parent
Main.Draggable = true
local ButtonEvents = Main.ButtonsEvents
local EmergencyButton = Main.EmergencyButton
local EmergencyButtonEnabled = Main.EmergencyButtonEnabled
local AxleButton = Main.AxleButton
local RotarySelectorSwitchTwisting = AxleButton.RotarySelectorSwitchTwisting
local SelectedAxleValue = Main.SelectedAxleValue
local PlusButton = Main.PlusButton
local PlusButtonEvent = ButtonEvents.PlusButtonEvent
local MinusButton = Main.MinusButton
local MinusButtonEvent = ButtonEvents.MinusButtonEvent
--[[ EMERGENCY BUTTON ]]
EmergencyButton.MouseButton1Click:Connect(function()
if EmergencyButtonEnabled.Value == false then
EmergencyButtonEnabled.Value = true
else
print("Emergency button already enabled!")
end
end)
EmergencyButton.MouseButton2Click:Connect(function()
if EmergencyButtonEnabled.Value == true then
EmergencyButtonEnabled.Value = false
else
print("Emergency button already disabled!")
end
end)
--[[ AXLE BUTTON ]]
AxleButton.MouseButton1Click:Connect(function()
local X = 0
local Y = 35
local Z = 75
if AxleButton.Rotation == X then
wait(0.25)
AxleButton.Rotation = Y
SelectedAxleValue.Value = "Y"
RotarySelectorSwitchTwisting:Play()
elseif AxleButton.Rotation == Y then
wait(0.25)
AxleButton.Rotation = Z
SelectedAxleValue.Value = "Z"
RotarySelectorSwitchTwisting:Play()
end
end)
AxleButton.MouseButton2Click:Connect(function()
local X = 0
local Y = 35
local Z = 75
if AxleButton.Rotation == Z then
wait(0.25)
AxleButton.Rotation = Y
SelectedAxleValue.Value = "Y"
RotarySelectorSwitchTwisting:Play()
elseif AxleButton.Rotation == Y then
wait(0.25)
AxleButton.Rotation = X
SelectedAxleValue.Value = "X"
RotarySelectorSwitchTwisting:Play()
end
end)
local RepeatValue = script.RepeatValue
--[[ PLUS BUTTON / KEYCODE ]]
PlusButton.MouseButton1Down:Connect(function()
RepeatValue.Value = true
while RepeatValue.Value == true do
if EmergencyButtonEnabled.Value == false then
PlusButtonEvent:FireServer(SelectedAxleValue.Value)
else
print("Emergency button active!")
end
wait(0.01)
end
end)
PlusButton.MouseButton1Up:Connect(function()
RepeatValue.Value = false
end)
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.KeypadPlus then
RepeatValue.Value = true
while RepeatValue.Value == true do
if EmergencyButtonEnabled.Value == false then
PlusButtonEvent:FireServer(SelectedAxleValue.Value)
else
print("Emergency button active!")
end
wait(0.01)
end
end
end)
UIS.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.KeypadPlus then
RepeatValue.Value = false
end
end)
--[[ MINUS BUTTON / KEYCODE ]]
MinusButton.MouseButton1Down:Connect(function()
RepeatValue.Value = true
while RepeatValue.Value == true do
if EmergencyButtonEnabled.Value == false then
MinusButtonEvent:FireServer(SelectedAxleValue.Value)
else
print("Emergency button active!")
end
wait(0.01)
end
end)
MinusButton.MouseButton1Up:Connect(function()
RepeatValue.Value = false
end)
UIS.InputBegan:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.KeypadMinus then
RepeatValue.Value = true
while RepeatValue.Value == true do
if EmergencyButtonEnabled.Value == false then
MinusButtonEvent:FireServer(SelectedAxleValue.Value)
else
print("Emergency button active!")
end
wait(0.01)
end
end
end)
UIS.InputEnded:Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.KeypadMinus then
RepeatValue.Value = false
end
end)
The remote events script:
--[[ CNC MACHINE BUTTONS EVENTS ]]
local MachineHead = game.Workspace.MachineHead
local Main = script.Parent
local ButtonsEvents = Main.ButtonsEvents
local PlusButtonEvent = ButtonsEvents.PlusButtonEvent
local MinusButtonEvent = ButtonsEvents.MinusButtonEvent
local EmergencyButtonEnabled = Main.EmergencyButtonEnabled
PlusButtonEvent.OnServerEvent:Connect(function(Player, SelectedAxleValue)
if SelectedAxleValue == "X" then
MachineHead.Position = Vector3.new(MachineHead.Position.X + 0.1, MachineHead.Position.Y, MachineHead.Position.Z)
elseif SelectedAxleValue == "Y" then
MachineHead.Position = Vector3.new(MachineHead.Position.X, MachineHead.Position.Y + 0.1, MachineHead.Position.Z)
elseif SelectedAxleValue == "Z" then
MachineHead.Position = Vector3.new(MachineHead.Position.X, MachineHead.Position.Y, MachineHead.Position.Z + 0.1)
end
end)
MinusButtonEvent.OnServerEvent:Connect(function(Player, SelectedAxleValue)
if SelectedAxleValue == "X" then
MachineHead.Position = Vector3.new(MachineHead.Position.X - 0.1, MachineHead.Position.Y, MachineHead.Position.Z)
elseif SelectedAxleValue == "Y" then
MachineHead.Position = Vector3.new(MachineHead.Position.X, MachineHead.Position.Y - 0.1, MachineHead.Position.Z)
elseif SelectedAxleValue == "Z" then
MachineHead.Position = Vector3.new(MachineHead.Position.X, MachineHead.Position.Y, MachineHead.Position.Z - 0.1)
end
end)