I’m trying to replicate a part’s CFrame change from a RenderStepped loop to appear on the server/all clients. My problem is that I can’t use RenderStepped in the server so the server can’t see the changes and the CFrame changes only appear for me, but not for the server. I’ve also tried a while loop, but the problem with that is that the changes don’t appear to neither me nor the server. Is there any solution to this?
I honestly don’t see why you trying to use a while loop on the server at all. The client is firing all of the needed parameters to the server each update, so why not just do it without the while loop?
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Origin = ReplicatedStorage.Origin
local End = ReplicatedStorage.End
local isProtonFiring = false
function startBeam(player, RootPart, MouseHit)
Origin.Parent = RootPart
End.Parent = game.Workspace
isFiringProton = true
Origin.CFrame = ProtonGun.Handle.StreamPos.CFrame
End.CFrame = MouseHit
end
Remotes.StopBeam.OnServerEvent:Connect(function()
Origin.Parent = ReplicatedStorage
End.Parent = ReplicatedStorage
isFiringProton = false
end)
Also, you will need to define ProtonGun within the server script.
I had this idea:
Create a client-replicated part sometime before shooting the beam.
Tell server to tell other clients to begin shooting the beam as well.
Have the other clients generate a beam from RootPart to ReplicatedPart.
Don’t generate the beam on the server.
im not experienced in runservice specifically, but heartbeat is just a runservice loop you could use on the server. Its really fast but not as fast as render stepped.
The heartbeat function fires every frame. If your game is running on 40 fps heartbeat will run 40 times per second and the argument (parameter) would be 1/40th of a second
I got the End part of the beam to work on the server and client! Only problem is now the Origin part (where the ProtonGun is) doesn’t follow me neither on client nor on the server.
I got the Origin following me on the server and client. Only problem is that on client, I see the Origin’s part position change late, while the server is on track and it looks fine. Is there a way so I can make it even on both the client and server?
That’s why I said to generate the beam on the clients. That or generate it on the client who is firing the beam and have it replicate to the server and then to other clients.
The player firing the beam is replicating the character to the server and then the server is taking that replication and generating the beam which then replicates back to the player. This creates the moment of lag you are seeing.
local connections = {}
local rs = game:GetService("RunService")
Remotes.StartBeam.OnClientEvent:Connect(function(originPart, endPart, id)
connections[id] = rs.RenderStepped:Connect(function(dt) -- we use RenderStepped so it looks smooth while animating
-- generate beam from originPart to endPart
end)
end)
Remotes.StopBeam.OnClientEvent:Connect(function(id)
if connections[id] then connections[id]:Disconnect(); connections[id] = nil end
end)
-- userinputservice or something to trigger beam
-- also define mouse
Remotes.StartBeam:FireServer(originPart, endPart)
rs.Stepped:Connect(function(time, dt) -- we use Stepped because it changes with the
-- character, which is the physics update.
endPart.CFrame = mouse.Hit -- use mouse here so the endPart is being moved to where the mouse is
--, which will replicate to the server along with your character (so there's no weird lag offsetting the beam)
end)
I’m sorry sometimes I feel like speaking code is easier to understand. The basic idea is that the server simply tells the other clients when to start or stop generating the beam. And then the clients have to generate the beam and the client shooting the beam has to tell everybody somehow where the end/origin is supposed to be. Since the originPart is basically attached to the character, it will be replicated easily. But you have to manually move the mouse’s position to get the endPart to replicate that position.
You could alternatively spam a remote event with the mouse’s position.
It doesn’t seem to be working. The endPart updates on the client, but not on the server, and the originPart doesn’t update at all on the client nor server. And the endPart and originPart don’t appear on the server.
Here’s my code (contains server, client, and module script):
If it a replication issue then it’s because of network ownership. You have to set the network ownership of the parts to the client changing them. You have to do this from the server. Also the parts can’t be anchored when you do this.