Hello!
I’m currently making a switch that holds two basic functions:
Click left/right to turn it on/off respectively, as well as drag the switch to whichever side you want. The issue is that when the person goes to drag it, the switch doesn’t snap to the other side like it should.
In this video, “on” or “off” is printed whenever the player puts their mouse inside of the off/on boundary, however, when the mouse is held down, and the “canHold” boolean’s value is true, it doesn’t appear to work.
My script:
local UIS = game:GetService("UserInputService")
script.Parent.On.MouseButton1Up:Connect(function()
script.Parent.Off.Container.Visible = false
script.Parent.On.Container.Visible = true
end)
script.Parent.Off.MouseButton1Up:Connect(function()
script.Parent.On.Container.Visible = false
script.Parent.Off.Container.Visible = true
end)
script.Parent.Off.MouseEnter:Connect(function()
print("off")
if script.Parent.canHold.Value == true then
script.Parent.Off.Container.Visible = true
script.Parent.On.Container.Visible = false
print("off")
end
end)
script.Parent.On.MouseEnter:Connect(function()
print("on")
if script.Parent.canHold.Value == true then
script.Parent.Off.Container.Visible = false
script.Parent.On.Container.Visible = true
print("on")
end
end)
UIS.InputBegan:Connect(function(key)
if key.UserInputType == Enum.UserInputType.MouseButton1 then
local mouse = game.Players.LocalPlayer:GetMouse()
local x = mouse.X
local y = mouse.Y
local objects = game.Players.LocalPlayer.PlayerGui:GetGuiObjectsAtPosition(x, y)
for i,v in pairs(objects) do
if v == script.Parent.SwitchBounds then
print("Bounds")
script.Parent.canHold.Value = true
break
end
end
end
end)
UIS.InputEnded:Connect(function(key)
if key.UserInputType == Enum.UserInputType.MouseButton1 then
if script.Parent.canHold.Value == true then
script.Parent.canHold.Value = false
end
end
end)
Explorer (control script is highlighted):