Hi, just trying to figure out how to replicate the moving of an object with the players mouse location, that I have on client too server. Basicially wherever the player moves there mouse a “part” moves too, it works on client, just not sure how to make it do the same thing on the server because I need the players mouse for it, heres the code
while true do
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
game.Workspace["Stuffed Animal"]:SetPrimaryPartCFrame(CFrame.new(Mouse.hit.p) + Vector3.new(0,1.5,0))
This basicially just moves a stuffed animal wherever the players mouse is, Im just trying to figure out how to make it work on server, because it needs the players mouse in a while true do loop to keep following the players mouse. thanks
There are a couple possible solutions to this that I’m aware of, but some are more suitable than others depending on what the purpose of this is.
If the CFrame of the part is strictly important for other players either due to its collision or something else that isn’t purely visual, then you would need to use remotes in order to communicate to the server where the part should be.
This can cause bandwidth issues if done poorly, so be careful with how you set up any remote events/functions. As long as you aren’t firing them too rapidly and you’re only sending CFrame/positional values a few times per second, this shouldn’t have too much of an impact on gameplay. You probably shouldn’t do this 60 times per second with a RunService event because it will definitely cause problems if remotes are sending CFrame information that often. Though due to having less frequency, it will appear a bit choppy (e.g. using a remote an entire 15 times per second would make the part appear to be moving at around 15 FPS). You can hopefully find a good balance that allows the part to appear to move somewhat smoothly without causing any bandwidth issues.
However, if this is entirely for visuals or cosmetic purposes and it’s acceptable for the part to be unanchored with no collisions on the server-side (at least while it’s being moved), you can technically avoid using remotes by simply setting the network owner of the part with :SetNetworkOwner(). This will allow the player to control the part’s movement with a local script and it will still replicate to other clients. This is much less complicated than using remotes and it looks something like this:
local player = game.Players.BlahBlahBlah
local part = Instance.new("Part")
part.Name = "Part" .. player.Name --simple tagging method for sake of example
part.Parent = workspace
part:SetNetworkOwner(player) --player that has control
local player = game.Players.LocalPlayer
local part = workspace:WaitForChild("Part" .. player.Name)
--the rest of your current code can be fit into here to move 'part'
Remotes can be a bit tricky for some people and code is all about problem-solving. Be sure to try different things until you’re happy with the result. These are just two potential solutions and there are likely more that I haven’t thought of.
I’ve been scripting for quite a while, But I took a really big break hence why I forgot how to do some of these things, SetNetworkOwner is entirely what I was looking for, thank you SO much, not sure what I would’ve done without this reply, You saved me a ton of time thank you so much!
Hmm, Okay actually, sorry to bother you again lol, But I guess the anchoring sorta is an issue, The part thats being moved kinda has to be anchored so that it can move following your mouse without it looking weird, Do you know any way around this? I’m guessing I’ll have to use a remote after all instead of set network owner? I’m just not sure how to go about using a remote because the part needs to constantly be following the players mouse Any advice? I’d like to use network ownership, but The part has to stay on the persons mouse, and Im not sure how to achieve that if the parts not anchored. Thanks in advance!