Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would i make objects dragable by players in game?

Asked by 5 years ago

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?

0
do you want it to be dragable to where the player want or like side to side? asdfghjk9019 225 — 5y

1 answer

Log in to vote
0
Answered by
Mr_Unlucky 1085 Moderation Voter
5 years ago
Edited 5 years ago
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.

0
This script is also client-sided, however if you want to make it replicate to the server you can use RemoteEvents. Mr_Unlucky 1085 — 5y
0
So far I've not been able to get this to work. maxbthecool3 6 — 5y
0
I've tried this myself, make a local script and put this into the StarterPlayerScripts thingy Mr_Unlucky 1085 — 5y
0
I see what I did wrong thanks! maxbthecool3 6 — 5y
View all comments (2 more)
0
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. Mr_Unlucky 1085 — 5y
0
Alright. maxbthecool3 6 — 5y
Ad

Answer this question