Problem with Tween and Ray

So basically i am trying to do some moving platforms and i want the player locked to them while there is a tween so i found this script, it’s the first time i use Ray, but i’ve still understood it.
But there is a problem and i can’t find out how to fix it. I don’t really know how to explain it here is the video.

the script is this one

local tableee = {"1stblock", "2ndblock", "3rdblock", "4rdblock", "5thblock", "6thblock"}
local Players = game:GetService("Players")
	local player = game.Players.LocalPlayer
	local RunService = game:GetService('RunService')

	local LastTrainCFrame

	local Function


	Function = RunService.Heartbeat:Connect(function()

		--------------------------------------------------------------- CHECK PLATFORM BELOW

		local RootPart = player.Character:WaitForChild("HumanoidRootPart")

		local Ignore = player.Character

		local ray = Ray.new(RootPart.CFrame.p,Vector3.new(0,-50,0))

		local Hit, Position, Normal, Material = workspace:FindPartOnRay(ray,Ignore)

		if Hit and table.find(tablee, Hit.Name) then -- Change "RaftTop" to whatever the moving part's name is
			print("yez")

			--------------------------------------------------------------- MOVE PLAYER TO NEW POSITON FROM OLD POSITION

			local Train = Hit
			if LastTrainCFrame == nil then -- If no LastTrainCFrame exists, make one!
				LastTrainCFrame = Train.CFrame -- This is updated later.
			end
			local TrainCF = Train.CFrame 

			local Rel = TrainCF * LastTrainCFrame:inverse()

			LastTrainCFrame = Train.CFrame -- Updated here.

			RootPart.CFrame = Rel * RootPart.CFrame -- Set the player's CFrame
			--print("set")

		else
			LastTrainCFrame = nil -- Clear the value when the player gets off.

		end


	end)

And then there is a tween for every platform

local tween1 = game:GetService("TweenService"):Create(blockone, TweenInfo.new(4,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut, -1, true, 2), {CFrame = game.Workspace:WaitForChild("1stblockCFRAme").CFrame})
	tween1:Play()
	local tween2 = game:GetService("TweenService"):Create(blocktwo, TweenInfo.new(1,Enum.EasingStyle.Exponential,Enum.EasingDirection.InOut, -1, true, 2), {CFrame = game.Workspace:WaitForChild("2ndblockCFRame").CFrame})
	tween2:Play()
	local tween3 = game:GetService("TweenService"):Create(blockthree, TweenInfo.new(3,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut, -1, true, 1), {CFrame = game.Workspace:WaitForChild("3rdblockCFRAmw").CFrame})
	tween3:Play()
	local tween4 = game:GetService("TweenService"):Create(blockfour, TweenInfo.new(5,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut, -1, true, 1), {CFrame = game.Workspace:WaitForChild("4rdblockCFRAME").CFrame})
    tween4:Play()
	local tween5 = game:GetService("TweenService"):Create(blockfive, TweenInfo.new(2.5,Enum.EasingStyle.Bounce,Enum.EasingDirection.InOut, -1, true, .01), {CFrame = game.Workspace:WaitForChild("5thblockCFramE").CFrame})
	tween5:Play() 
	local tween6 = game:GetService("TweenService"):Create(block6, TweenInfo.new(4,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut, -1, true, .5), {CFrame = game.Workspace:WaitForChild("6thblockCFRA").CFrame})
	tween6:Play()
	

The script are in starterPlayerScripts btw. Every answer is welcome and please, since i’m still beginner with Ray, i’d be grateful if you could explain me your answer/s :))

1 Like

Also i tried using Prismatic constrains, but i don’t want to make a loop for every platform for performance reasons, also i think tween is more “customizable”.

I would get rid of the script you showed and instead change the assembly linear velocity of the moving platform. The problem is that when you tween it is not considered a physics based movement so it will have no inertia and will not move a player standing on it. You would have to make a loop that sets the assembly linear velocity of the part to the direction * speed.

1 Like

I found someone that had done something similar to what you want, which essentially does what @RiccoMiller suggested.

I didn’t look in-depth, but it looks well-detailed, and explains how everything works, so if you take a look it might help you modify your code and make it work for what you need.

3 Likes

Thank you so much for your answer. The “problem” is that since i want every tween different (and they will be like 10 tweens)(different time, different delay time and so on) i would have to make several runservice functions, and it is not good for game performance. if you know a way to make it with only 1 runservice function could you tell it to me please?

Same reply. @SigmaTech used a runservice function, but i would have to make several runservice functions for every tween and (even if it is client-side) it is not SO good for game performance . Tbh i’m not able to use maybe only 1 runservice function for all the tweens. If you have any clue/idea feel free to tell me :))… If there is no solution, maybe i’ll try to use all the runservices, but tbh i don’t have good hopes for this.

Please use CollectionService and instantiate your tweening parts from a component or a class.
There is a principle in programming called DRY: “Do not repeat yourself”.
Define the procedure once and use it for many instances.

Sleitnick’s Component class also subsequently supports StreamingEnabled.

In no implementation should you be writing these RunService loops more than once!