Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is it possible to have a remote event work with RenderStepped?

So I'm attempting to use a RemoteEvent to tween a joint from a server script and the loop steps 1/60th of a second because it's connected to RenderStepped, but for some reason the function that tweens the joint tweens the joint in a very choppy manner instead of smoothly like how I want it to run, is there any reason for this? Or do RemoteEvents just not work with RenderStepped? Thanks.

Here's the remoteEvent code:

local lerpCF = script:WaitForChild("lerpCF")
function lerpCF.OnServerInvoke(_, Joint, Prop, startCF, endCF, Alpha)
	Joint[Prop] = startCF:lerp(endCF, Alpha)
end

Here's a tweenJoint code:

function tweenJoint(Joint, newC0, newC1, Alpha, Duration)
	spawn(function()
		local newCode = math.random(-1e9, 1e9)
		local tweenIndicator = createTweenIndicator:InvokeServer(Joint, newCode)
		if Duration <= 0 then
			if newC0 then Joint.C0 = newC0 end
			if newC1 then Joint.C1 = newC1 end
		else
			local Increment = 1.5 / Duration
			local startC0 = Joint.C0
			local startC1 = Joint.C1
			local X = 0
			while true do
				RS.RenderStepped:wait()
				local newX = X + Increment
				X = (newX > 90 and 90 or newX)
				if tweenIndicator.Value ~= newCode then break end
				local T = tick()
				if newC0 then lerpCF:InvokeServer(Joint, "C0", startC0, newC0, Alpha(X)) end
				if newC1 then lerpCF:InvokeServer(Joint, "C1", startC1, newC1, Alpha(X)) end
				print(tick() - T) --This prints some number near 0.03, which slows down the loop
				if X == 90 then break end
			end
		end
		deleteTweenIndicator:InvokeServer(tweenIndicator, newCode)
	end)
end

1 answer

Your problem is latency. It's always a bit laggy looking when you tell server to create an instance (well more like an instance that gets affected by physics) which needs to get replicated to all the clients. You should be handling this client side.

SERVER

function lerpCF.OnServerInvoke(...)
    lerpCF:InvokeAllClients(...) -- apparently there's no such function but as stated on the last line, you should use a RemoteEvent
end

CLIENT


lerpCF.OnClientInvoke = function(otherplr,Joint, Prop, startCF, endCF, Alpha)
if otherplr ~= localplayer then -- we don't want to duplicate the animation
--you might want to check if the target of animating is close enough to animate or just lower the quality depending on the distance
----animate
end
end

while true do
RS.RenderStepped:wait()
--tell server to tell other machines to animate the exact same way 
--animate this client's side because it's the most important
end

I use this method with projectiles and other visual stuff. It allows easy manipulating to the replicated object depending on the distance and other variables.

Also that's RemoteFunction, you aren't returning anything so I'd suggest using an event instead.

2 comments

0

This helps, but here's the thing though, this server script is in 1 tool that each player will have, and same with the client script. I want the client to have the joint tween at 1/60th of a second, but it's fine if other players see a slightly slower speed in tweening. So will this method that you provided allow me to tween joints at a 1/60th speed to the client and possibly to the server as well

0

The way I set up my scripts is having one server script inside SSS which handles all the replication stuff (mostly just telling clients to do this and that) and all the RemoteEvents inside ReplicatedStorage because there's no need for a new event&script instance for each client which I from your setup understood you're doing. To answer your question: yes, it will. There's no point doing it server

This discussion is preserved as it was. New answers, comments, and votes are closed.