Client Side Moving Platforms

Hi everyone!

I am trying to create a moving platform for a platformer game. I want the platform movement to be client side. Currently, my platform in the workspace has a script attached that runs this code (in the real project I have this ran from a ModuleScript).

local Platform = script.Parent

function Slide(Platform)
	local SlideEvent = PlatformEvents:WaitForChild("SlideEvent")
	local SlideDistance = Platform:GetAttribute("SlideDistance") or 10
	local SlideTime = Platform:GetAttribute("SlideTime") or 2
	local function onPlayerAdded(Player)
		SlideEvent:FireClient(Player, Platform, SlideDistance, SlideTime)
	end
	Players.PlayerAdded:Connect(onPlayerAdded)
end

Slide(Platform)

I have a RemoteEvent in ReplicatedStorage that a LocalScript in StarterPlayerScripts is listening to with this code

local function Slide(Platform, SlideDistance, SlideTime)
	local TweenService = game:GetService("TweenService")
	local RunService = game:GetService("RunService")
	-- tween the platform movement
	local SlideTweenInfo = TweenInfo.new(SlideTime, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut, -1, true)
	local Tween = TweenService:Create(Platform, SlideTweenInfo, {Position = Platform.Position * Vector3.new(SlideDistance, 0, 0)})
	Tween:Play()
	-- update velocity with RunService
	local PrevPosition = Platform.Position
	RunService.Stepped:Connect(function(_, deltaTime)
		local CurrentPosition = Platform.Position
		local DeltaPosition = CurrentPosition - PrevPosition
		local Velocity = DeltaPosition / deltaTime
		Platform.AssemblyLinearVelocity = Velocity
		PrevPosition = CurrentPosition
	end)
end
SlideEvent.OnClientEvent:Connect(Slide)

The error I am having is that when I try to test the game, the LocalScript sees the platform parameter as nil and does nothing. I think this is to do with the player loading before the workspace, so when we call the slide function the platform has not loaded in yet and therefore we send nil. I have tried only sending the event when the player character loads, but I had the same issue. I considered running FireAllClients() on the event rather than just one client but this has the issue of any new player joining after not seeing the platforms moving.

How do I get this to work properly? Thanks in advance for help!

Could you directly access the platform from the LocalScript temporarily to see if that works? I think this probably has to do with the instance being passed from the server to the client.

1 Like

This feels like another one of these Streaming Enabled-induced fever dreams. It takes one heartbeat for instances to replicate to players after they’re put somewhere player-visible, so it’s easy to fire a remote event referencing an instance the client doesn’t see yet.

One way: disable it

Another way: engineer the localscript in such a way that it can get some indirect reference to the platform and activate it when it shows up (collectionservice, attributes, anything labeled a platform slides)
edit: if the platform streams out and back in, it may get a second tween and stepped connection unless the script was inside the platform and therefore gets disabled/cleaned up with it

Yet another: straight up only runs only when the platform has replicated. My favorite is just putting a Client script in the part and either dropping the contents of Slide() in there or pinging some other system in the game to activate the platform.

The latter two are a pain in the rear obviously and don’t have the same ergonomics as just firing a remote with all the necessary data. I honestly haven’t had to deal with or work around Streaming Enabled myself, so I don’t have anything else to say if it was enabled intentionally. Actually nevermind your server script already gets all of that from Attributes, so nothing major needs to change

2 Likes

Thanks for the feedback everyone. I tried directly accessing the platform from the localscript by using CollectionService and tags, then calling the tween function once the player character is loaded in the localscript. This seems to work!

In my server-side script on the platform I just add a “Slide” tag to the platform, then in the localscript I start the tween on all platforms with the slide tag by accessing them with the CollectionService.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.