Part Position = Mouse location

Hello there!

I am doing a Town and city game right now and I am planning to make a placeble furniture system. Can you guys help me? I need to know how to make this: When the part was clicked (click Detector) the part is always at the mouse location, and if it is clicked again. It stops in the location when the mouse was last seen before clicking it again.

Thanks for help!

Hardcorowy101

3 Likes

You could make it so when you click on a part, it triggers a Client RemoteEvent to connect a RenderStepped event that moves the part to your mouse position every frame if the connection was not already made, and when you press it again, it will fire the same event again, but this time, since a connection is made, you could just make it Disconnect the event

2 Likes

Yeah, but i never used Remotes Events and I don’t know how to connect them to player mouse. Also thanks that you are here again.

Inhale

In the shortest summary possible: “RemoteEvents are basically 1 way transportation between both sides of the game (Client & Server)”

To explain further, you can send data from 1 section, then recieve the “said” data from another section when you connect the Event (Or in this case, the Mouse’s Position)

2 Likes

Basically, Put a remoteEvent in ReplicatedStorage, and make a localscript where it can be ran, such as StarterPlayerScripts or StarterGui, and connect it with an OnClientEvent and do the code that does this

  • Is an event already connected?
  • No, make a RenderStepped event that moves the part to the mouse every frame
  • Yes, disconnect the event which will stop the part from moving
1 Like

I know that this sounds annoing, but I am not scripter and I don’t know how the remoteEvents or scripts connected to remoteEvent work. :slightly_frowning_face:

We’ll start from the very basics,

Firstly, you’d put a RemoteEvent in ReplicatedStorage, just this occasion, we’ll keep it called RemoteEvent. First we’ll need to make it connect to an event, and it’s simple! Put a localscript somewhere that it can be activated, I recommend StarterGui or StarterPlayerScripts, and we’ll begin our first few lines

local event = game.ReplicatedStorage.RemoteEvent

event.OnClientEvent:Connect(function(part) --So we know what part to move
	
end)

Now, we need the code to be able to disconnect and reconnect an event, for this, we need a variable outside of the event to store the Event in

local event = game.ReplicatedStorage.RemoteEvent
local connection = nil

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local RunService = game:GetService("RunService")

event.OnClientEvent:Connect(function(part) --So we know what part to move
	if connection then
		connection:Disconnect() --Connect exists so we get rid of it
	else
		connection = RunService.RenderStepped:Connect(function()
			part.Position = mouse.Hit.Position
		end)
	end
end)

That’s the basis of it, now i nyour ClickDetector script, just make it Fire the event via game.ReplicatedStorage.RemoteEvent:FireClient() and pass in the player the MouseClick Events gives you

You will have to write some code to prevent the part from going too far away from you if you move your mouse away a lot

Typos

2 Likes

this should help

it helped me a lot when i was scripting

Doesn’t work /: Just nothing happens.

1 Like

Could you try this and see what gets outputted?

local event = game.ReplicatedStorage.RemoteEvent
local connection = nil

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local RunService = game:GetService("RunService")
print("Script Running")

event.OnClientEvent:Connect(function(part) --So we know what part to move
	if connection then
        print("Disconnected the event!")
		connection:Disconnect() --Connect exists so we get rid of it
	else
		connection = RunService.RenderStepped:Connect(function()
			part.Position = mouse.Hit.Position
            print("Mouse's Position: "..mouse.Hit.Position)
		end)
	end
end)

Still nothing happends when i click it.

Chances are it’s probably happening on the server side then

What’s your current code for the ClickDetector script?

script.Parent.MouseClick:Connect(function()
game.ReplicatedStorage.RemoteEvent:FireClient()
end)

Ah, that’s the issue
You didn’t even define anything inside the parameters of your MouseClick Event

Try this:

script.Parent.MouseClick:Connect(function(PlayerWhoClicked)
    game.ReplicatedStorage.RemoteEvent:FireClient(PlayerWhoClicked)
end)

Yeah, I changed it but still nothing.

Do you know where your Output is inside Studio?

I know bro, I am not new to Roblox Studio.

Okay then just checking are you recieving anything from inside there? Or is it empty?

Just nothing when i click it. @Jackscarlett