Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would i make objects dragable by players in game?

1 answer

✓ Accepted 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

0

This script is also client-sided, however if you want to make it replicate to the server you can use RemoteEvents.

0

So far I've not been able to get this to work.

0

I've tried this myself, make a local script and put this into the StarterPlayerScripts thingy

0

I see what I did wrong thanks!

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.

0

Alright.

This discussion is preserved as it was. New answers, comments, and votes are closed.