In my Roblox game i have a secret basement in a house. The door is covered by a rug on the floor and i want players to be able to move the rug by clicking or dragging it. How would i make this possible?
local Player = game:GetService("Players").LocalPlayer local Mouse = Player:GetMouse() local CurrentTarget local MouseDown function dragObject() if MouseDown and CurrentTarget ~= nil and CurrentTarget == workspace.Rug then local Coordinates = Mouse.Hit workspace.Rug.Position = Vector3.new(Coordinates.X,.5,Coordinates.Z) end end function mouseDown() MouseDown = true CurrentTarget = Mouse.Target end function mouseUp() MouseDown = false CurrentTarget = nil end Mouse.Button1Down:Connect(mouseDown) Mouse.Button1Up:Connect(mouseUp) Mouse.Move:Connect(dragObject)
Now even though I haven't tested if this works, I'll explain the code.
Basically, we assign multiple variables, one for the player, one for the mouse, and two for both the current part being dragged and if the mouse is down. We have a function that checks if MouseDown is true and if there is a target, and we also check if CurrentTarget is the rug. If that's the case we assign another variable for the coordinates for where the mouse is at in the workspace. We then change the rug's position using Vector3.new() and the coordinates of Mouse.Hit. We also create more functions for making the mousedown true/false and assigning new values for the CurrentTarget. Finally, we connect each individual function to the events.
Edit: Improved the code so that the coordinates for the Y axis is not modified.