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.
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
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)
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
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
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)