I want to have Models appear where the player is hovering with their mouse, a preview for when you’re building like I have seen for a lot of games.
I’m encountering 3 main issues:
- Replication doesn’t really work (the reason why I am posting this)
- Probably related to the first one, other Parts welded to the Part I am moving don’t move with it
- Gravity keeps affecting it, even when I’m setting it every frame
I have tried quite a lot of approaches, I initially simply did everything on the client (cloned from ReplicatedStorage into workspace, set Position from client), however welded Parts didn’t work, which I assumed was because of Replication. Then, I did the same thing except cloned from the Server and sent over the Position so the server can set it, the latency was not fun and I changed it. Now I have essentially my first implementation, except I’m spawning and setting Network Ownership of the part to the client on the server. It didn’t really help much, and now the parts are also affected by very buggy gravity.
My giver script:
local clone = Preview:Clone()
clone.Parent = player.Character
clone:SetNetworkOwner(player)
My client script (using RunContext Client)
local Players = game:GetService("Players")
local UserInputServce = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local preview = script.Parent
if not preview:IsDescendantOf(workspace) then
return
end
local character = preview.Parent
if not character then
return
end
local player = Players:GetPlayerFromCharacter(character)
if (not player) or player ~= Players.LocalPlayer then
return
end
local camera = workspace.CurrentCamera
local params = RaycastParams.new()
params.FilterDescendantsInstances = { character }
params.FilterType = Enum.RaycastFilterType.Exclude
local mouse = player:GetMouse()
mouse.Button1Up:Connect(function()
preview:Destroy()
end)
local lastPos = Vector3.zero
RunService.PreSimulation:Connect(function()
local mousePos = UserInputServce:GetMouseLocation()
local unitRay = camera:ScreenPointToRay(mousePos.X, mousePos.Y)
local result = workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, params)
if result then
preview.Position = result.Position
else
preview.Position = Vector3.zero -- because of gravity
end
end)
If there is a way for welds (or a similar way to group parts together) to work clientside without it replicating I’ll take it, replication is not really important to me. Also the raycast hit positions are slightly offset from where the mouse actually is, which is not that big of a deal but still annoying.
Any help would be appreciated.