Hello, I am a beginner in scripting. I attempted to use a local script to handle moving platforms, but my knowledge is limited
.
-
What do you want to achieve?
A moving platform that uses Prismatic Constraints. When the client touches it, the platform will only move for the client and no one else. -
What is the issue?
Upon touching (using local script), after a delay, it moves for all clients. Expected behavior for scripts. -
What solutions have you tried so far?
Run Service seems like a good idea, but I dont know how to best utilize it. I want to see if its possible to not use remote events to minimize lag.
Here’s the model link for anyone to try: MovingPlatform - Roblox
Script 1: Uses a script for server usage
local Players = game:GetService("Players")
local start = script.Parent:WaitForChild("Start");
local finish = script.Parent:WaitForChild("Finish");
local Platform = script.Parent:WaitForChild("MovingPart")
local DefaultPos = Platform.CFrame
local PrismaticConstraint = Platform:WaitForChild("PrismaticConstraint")
local Speed = Platform:WaitForChild("Speed")
PrismaticConstraint.Velocity = 0
local forward = false
local Moving = false
local function PlatformStart(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and Moving == false then
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
Moving = true
PrismaticConstraint.Velocity = Speed.Value
end
end
end
Platform.Touched:Connect(PlatformStart)
local function FinishTouch(hit)
if hit == Platform and forward == false then
forward = true
PrismaticConstraint.Velocity = -Speed.Value
end
end
finish.Touched:Connect(FinishTouch)
local function StartTouch(hit)
if hit == Platform and forward == true then
forward = false
PrismaticConstraint.Velocity = Speed.Value
end
end
start.Touched:Connect(StartTouch)
Script 2: An attempt for local script in StarterPlayerScripts
local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")
for _, inst in pairs(CollectionService:GetTagged("MovingPlatform")) do
if inst then
local start = inst:WaitForChild("Start");
local finish = inst:WaitForChild("Finish");
local Platform = inst:WaitForChild("MovingPart")
local DefaultPos = Platform.CFrame
local PrismaticConstraint = Platform:WaitForChild("PrismaticConstraint")
local Speed = Platform:WaitForChild("Speed")
PrismaticConstraint.Velocity = Speed.Value
local forward = false
local Moving = false
local function PlatformStart(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and Moving == false then
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player == Players.LocalPlayer then
Moving = true
PrismaticConstraint.Velocity = Speed.Value
end
end
end
Platform.Touched:Connect(PlatformStart)
local function FinishTouch(hit)
if hit == Platform and forward == false then
forward = true
PrismaticConstraint.Velocity = -Speed.Value
end
end
finish.Touched:Connect(FinishTouch)
local function StartTouch(hit)
if hit == Platform and forward == true then
forward = false
PrismaticConstraint.Velocity = Speed.Value
end
end
start.Touched:Connect(StartTouch)
end
end