I’m currently in the making of a feature where the client can drag objects around the map using their mouse but I’ve come across an issue. I want all movements to be visual to all players but I don’t know the correct way to do this. I’ve already tried to fire a remote event but firing as rapidly as I need will cause issues. I’ve also tried setting the Network ownership to the player but this does not work due to the objects being picked up are anchored. What would you say the best way I can achieve this is?
Local Script (Mouse movement):
local LocalPlayer = Players.LocalPlayer
local LocalMouse = LocalPlayer:GetMouse()
local MouseButton1Down = false
local MouseTarget
-- [[Functions]] --
local function inputBegan()
MouseTarget = LocalMouse.Target
if MouseTarget then
LocalMouse.TargetFilter = MouseTarget
if string.find(MouseTarget.Name, "Patty") then
if MouseTarget.Anchored then
if MouseTarget:GetAttribute("Draggable") then
MouseButton1Down = true
end
end
end
end
end
local function inputEnded()
if MouseTarget then
if string.find(MouseTarget.Name, "Patty") then
local target = MouseTarget
MouseButton1Down = false
MouseTarget = nil
LocalMouse.TargetFilter = nil
local RaycastParameters = RaycastParams.new()
RaycastParameters.FilterDescendantsInstances = {game.Workspace.Stove1.CookingRegion}
RaycastParameters.FilterType = Enum.RaycastFilterType.Exclude
local RayResults = workspace:Raycast(target.Position, (Vector3.new(target.Position.X, target.Position.Y - 50, target.Position.Z) - target.Position), RaycastParameters)
if math.round(RayResults.Distance) >= 1 then
target.Position = Vector3.new(target.Position.X, target.Position.Y - (RayResults.Distance - (target.Size.Y / 2)), target.Position.Z)
end
NetworkEvent:FireServer(target, target.CFrame)
end
end
end
local function renderStepped()
if MouseTarget and MouseButton1Down then
NetworkEvent:FireServer(MouseTarget, CFrame.new(Vector3.new(LocalMouse.Hit.Position.X, LocalMouse.Hit.Position.Y + 1, LocalMouse.Hit.Position.Z)))
end
end
-- [[Events]] --
LocalMouse.Button1Down:Connect(inputBegan)
LocalMouse.Button1Up:Connect(inputEnded)
RunService.RenderStepped:Connect(renderStepped)