I’m making hold and drag gui, watch this video then you can understand all.
I wanna that gui comes middle of mouse position.
I changed code some times and tried another method to drag it, but all become like this at end. I think middle position of gui is anchored to left end. I changed drag model to only 1 frame but it didn’t fixed.
code:
local Menu = script.Parent
local Main = Menu.Parent
local UserInputService = game:GetService("UserInputService")
local Mouse = game.Players.LocalPlayer:GetMouse()
local Placing
for i,c in ipairs(Menu:GetChildren()) do
if c:IsA("ImageButton") then
c.InputBegan:Connect(function(Input)
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
task.wait()
local Enemy = game.ReplicatedStorage.PlaceModel[c.Name]:Clone()
Enemy.Position = UDim2.new(0,Mouse.X,0,Mouse.Y)
Enemy.Parent = Main
Placing = Enemy
end
end)
end
end
Mouse.Move:Connect(function()
if Placing then
Placing.Position = UDim2.new(0,Mouse.X,0,Mouse.Y)
end
end)
UserInputService.InputBegan:Connect(function(Input,gpe)
if Placing and Input.KeyCode == Enum.KeyCode.R and not gpe then
Placing.Rotation += 90
end
end)
UserInputService.InputEnded:Connect(function(Input)
if Placing then
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
if Placing.Position.Y.Offset <= 400 then
local Model = game.ReplicatedStorage.Model[Placing.Name]:Clone()
Model.Parent = Main
Model.Position = Placing.Position
Model.Rotation = Placing.Rotation
end
Placing:Destroy()
Placing = nil
end
end
end)
2 Likes
this should be in #help-and-feedback #help-and-feedback:scripting-support could you move it?
Also, does this work?
local Menu = script.Parent
local Main = Menu.Parent
local UserInputService = game:GetService("UserInputService")
local Mouse = game.Players.LocalPlayer:GetMouse()
local Placing
for i, c in ipairs(Menu:GetChildren()) do
if c:IsA("ImageButton") then
c.InputBegan:Connect(function(Input)
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
task.wait()
local Enemy = game.ReplicatedStorage.PlaceModel[c.Name]:Clone()
local sizeX = Enemy.Size.X.Offset / 2
local sizeY = Enemy.Size.Y.Offset / 2
Enemy.Position = UDim2.new(0, Mouse.X - sizeX, 0, Mouse.Y - sizeY)
Enemy.Parent = Main
Placing = Enemy
end
end)
end
end
Mouse.Move:Connect(function()
if Placing then
local sizeX = Placing.Size.X.Offset / 2
local sizeY = Placing.Size.Y.Offset / 2
Placing.Position = UDim2.new(0, Mouse.X - sizeX, 0, Mouse.Y - sizeY)
end
end)
UserInputService.InputBegan:Connect(function(Input, gpe)
if Placing and Input.KeyCode == Enum.KeyCode.R and not gpe then
Placing.Rotation += 90
end
end)
UserInputService.InputEnded:Connect(function(Input)
if Placing then
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
if Placing.Position.Y.Offset <= 400 then
local Model = game.ReplicatedStorage.Model[Placing.Name]:Clone()
Model.Parent = Main
Model.Position = Placing.Position
Model.Rotation = Placing.Rotation
end
Placing:Destroy()
Placing = nil
end
end
end)
2 Likes
I tried it.
it didn’t fixed. still same bug
2 Likes
is your UI’s anchor point set to 0.5, 0.5?
2 Likes
it was 0,0 .
I changed it to 0.5,0.5 and 1,1 , 0.1,0.1 but they didn’t go middle, just go more far
1 Like
Can I see the UI hierarchy please?
3 Likes
if it is getting farther then you should just have to keep reducing the value to get it closer to the center of the mouse
1 Like
I got kind of a solution, first off, getting the mouse object is deprecated and out dated, you should be using UserInputService
for that, anyway, heres what I got:
--Get the Frame
local Frame = script.Parent.Frame
--Get Services
local RS = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
--Set Frames Anchor Point so it stays in the center
Frame.AnchorPoint = Vector2.new(0.5,0.5)
--Each frame, get the mouse location and set the frame's position to the mouse's position
RS.RenderStepped:Connect(function()
local Mouse = UIS:GetMouseLocation()
local ViewportX = workspace.CurrentCamera.ViewportSize.X
local ViewportY = workspace.CurrentCamera.ViewportSize.Y
Frame.Position = UDim2.fromScale(Mouse.X / ViewportX, Mouse.Y / ViewportY)
end)
1 Like
This worked exactly but it was enough to “local Mouse = UserInputService:GetMouseLocation()”
so this is full code
local Menu = script.Parent
local Main = Menu.Parent
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
--local Mouse = game.Players.LocalPlayer:GetMouse()
local Placing
for i,c in ipairs(Menu:GetChildren()) do
if c:IsA("ImageButton") then
c.InputBegan:Connect(function(Input)
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
task.wait()
local Enemy = game.ReplicatedStorage.PlaceModel[c.Name]:Clone()
local Mouse = UserInputService:GetMouseLocation()
Enemy.Position = UDim2.new(0,Mouse.X,0,Mouse.Y)
Enemy.Parent = Main
Placing = Enemy
RunService.RenderStepped:Connect(function()
local Mouse = UserInputService:GetMouseLocation()
--[[local ViewportX = workspace.CurrentCamera.ViewportSize.X
local ViewportY = workspace.CurrentCamera.ViewportSize.Y]]
--Enemy.Position = UDim2.fromScale(Mouse.X/ViewportX,Mouse.Y/ViewportY)
Enemy.Position = UDim2.new(0,Mouse.X,0,Mouse.Y)
end)
end
end)
end
end
--[[Mouse.Move:Connect(function()
if Placing then
Placing.Position = UDim2.new(0,Mouse.X,0,Mouse.Y)
end
end)]]
UserInputService.InputBegan:Connect(function(Input,gpe)
if Placing and Input.KeyCode == Enum.KeyCode.R and not gpe then
Placing.Rotation += 90
end
end)
UserInputService.InputEnded:Connect(function(Input)
if Placing then
if Input.UserInputType == Enum.UserInputType.MouseButton1 then
if Placing.Position.Y.Offset <= 400 then
local Model = game.ReplicatedStorage.Model[Placing.Name]:Clone()
Model.Parent = Main
Model.Position = Placing.Position
Model.Rotation = Placing.Rotation
end
Placing:Destroy()
Placing = nil
end
end
end)
thanks to him I could fix this problem.
2 Likes