I’m trying to create a drag effect so players can input their tools to the next tool upgrade.
Script
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
--
local AnvilGui = script.AnvilGui
local Frame = AnvilGui.Frame.Frame
local InputSlot = Frame.InputSlot
local OutputSlot = Frame.OutputSlot
--
local DraggableObject = require(script.DraggableObject)
local CurrentAction
local CurrentDrag
local ScreenSize = workspace.CurrentCamera.ViewportSize
local UpgradeLabel = script.UpgradeLabel
local Anvil = {}
local function Drag(button)
return function()
local mL = UserInputService:GetMouseLocation()
local scaleX = mL.X/ScreenSize.X
local scaleY = mL.Y/ScreenSize.Y
button.Position = UDim2.fromScale(scaleX,scaleY)
end
end
local function ButtonHold(button) -- Fix size of button holding
return function(actionName,inputState)
if inputState == Enum.UserInputState.Begin then
button.Parent = AnvilGui
RunService:BindToRenderStep("Drag",Enum.RenderPriority.Camera.Value - 1,Drag(button))
elseif inputState == Enum.UserInputState.End then
CurrentDrag:Disable()
RunService:UnbindFromRenderStep("Drag")
Anvil:CheckPosition(button)
end
end
end
function Anvil:CheckPosition(button)
local distance = (OutputSlot.AbsolutePosition - button.AbsolutePosition).Magnitude
if distance < 100 then
button.Parent = OutputSlot
elseif distance > 100 then
button.Parent = InputSlot
end
button.Position = UDim2.fromScale(0.5,0.5)
end
function Anvil:Init()
local button = self:CreateButton()
script.AnvilGui.Enabled = true
end
function Anvil:CreateButton()
local new = UpgradeLabel:Clone()
new.Parent = InputSlot
ContextActionService:BindAction("Mouse",ButtonHold(new),false,Enum.UserInputType.MouseButton1)
end
function Anvil:End()
ContextActionService:UnbindAction("Mouse")
CurrentDrag:Disable()
CurrentDrag = nil
end
return Anvil
I tried using absolute position only at first, but now I am just turning them into a fraction of the player’s screen so I can use scale instead.
The problems I have come across are that my cloned “UpgradeLabel” is tweeked in a random position when i’m dragging.
Right now I’m trying to get the button’s first original position and then add it into the equation.
Any ideas?