Issue with stopping prismatic force

I’ve been trying to create a moving platform that the player also moves along side with. From looking at the dev forum I found this old open source place that has a moving platform using a prismatic force.
https://www.roblox.com/games/2506819790/Platformer-movement

I want the platform to wait a couple of seconds before it moves. I’ve tried many different options such as setting the velocity to 0 for a bit or anchoring the platform for a bit and while these do stop the platform from moving, the player stops moving along side the platform.

Here’s the original code:

local SPEED = 10;

local start = script.Parent:WaitForChild("Start");
local finish = script.Parent:WaitForChild("Finish");

local attach0 = Instance.new("Attachment");
attach0.CFrame = CFrame.new();
attach0.Parent = script.Parent;

local attach1 = Instance.new("Attachment");
attach1.CFrame = finish.CFrame;
attach1.Parent = game.Workspace.Terrain;

local pris = Instance.new("PrismaticConstraint");
pris.ActuatorType = Enum.ActuatorType.Motor;
pris.MotorMaxForce = math.huge;
pris.Attachment0 = attach1;
pris.Attachment1 = attach0;
pris.Parent = script.Parent;

local forward = true;
pris.Velocity = SPEED;
attach1.CFrame = finish.CFrame

start.Touched:Connect(function(hit)
	if (hit == script.Parent and not forward) then
		forward = true;
		pris.Velocity = SPEED;
		attach1.CFrame = finish.CFrame
	end
end)

finish.Touched:Connect(function(hit)
	if (hit == script.Parent and forward) then
		forward = false;
		pris.Velocity = -SPEED;
		attach1.CFrame = start.CFrame
	end
end)

I’ve tried to look through the dev forum but most of the solutions have the actuator type set to servo and use target destination where as I want to uses parts as a way point system in the future.

This should work to achieve what you want, although personally I must admit I would have preferred to use a servo:

local SPEED = 10;
local DELAY = 2;

local start = script.Parent:WaitForChild("Start");
local finish = script.Parent:WaitForChild("Finish");

local attach0 = Instance.new("Attachment");
attach0.CFrame = CFrame.new();
attach0.Parent = script.Parent;

local attach1 = Instance.new("Attachment");
attach1.CFrame = finish.CFrame;
attach1.Parent = game.Workspace.Terrain;

local pris = Instance.new("PrismaticConstraint");
pris.ActuatorType = Enum.ActuatorType.Motor;
pris.MotorMaxForce = math.huge;
pris.Attachment0 = attach1;
pris.Attachment1 = attach0;
pris.Parent = script.Parent;

local forward = true;
pris.Velocity = SPEED;
attach1.CFrame = finish.CFrame

local prisThread = coroutine.create(function()
	pris.Velocity = 0;
	task.wait(DELAY);
	pris.Velocity = -SPEED;
end);

start.Touched:Connect(function(hit)
	if (hit == script.Parent and not forward) then
		forward = true;
		attach1.CFrame = finish.CFrame;

		coroutine.close(prisThread);
		prisThread = task.spawn(function()
			pris.Velocity = 0;
			task.wait(DELAY);
			pris.Velocity = SPEED;
		end);
	end
end)

finish.Touched:Connect(function(hit)
	if (hit == script.Parent and forward) then
		forward = false;
		attach1.CFrame = start.CFrame;

		coroutine.close(prisThread);
		prisThread = task.spawn(function()
			pris.Velocity = 0;
			task.wait(DELAY);
			pris.Velocity = -SPEED;
		end);
	end
end)

Important edit: @MightyKamblocks2000 I just made a correction to the code, it should work correctly now since I tested it in the place you provided

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