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?
How would i make objects dragable by players in game?
1 answer
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.
6 comments
This script is also client-sided, however if you want to make it replicate to the server you can use RemoteEvents.
So far I've not been able to get this to work.
I've tried this myself, make a local script and put this into the StarterPlayerScripts thingy
I see what I did wrong thanks!
Thanks for accepting my answer! Just keep in mind that any changes you do with the rug wont show for other people, so if you want that you should put remote events so that the position is updated.
Alright.
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Hmmm.... https://scriptinghelpers.org/blog/creating-a-furniture-placement-system
do you want it to be dragable to where the player want or like side to side?