Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Need add in script for Ontouch or Touched for moving platform?

Asked by 6 years ago

Currently the moving platform is moving back and forth continuously, what add in scripting would cause the platform to not start moving back and forth until touched or stepped on by the character and also stop when the character steps off the platform?


platform = game.Workspace.Stay1.Test6 bodyposition = script.Parent.BodyPosition finish = game.Workspace.Stay1.Finish start = game.Workspace.Stay1.Start while true do bodyposition.Position = start.Position wait(2.3) bodyposition.Position = finish.Position wait(2.5) end
0
I'm currently on holiday for another week and a bit, and don't have access to my computer or notes I wrote for this earlier until I get back. If you don't have an answer by then I will be able to answer then, sorry about that vector3_zero 1056 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

To do this cleanly involves quite a bit of code, but i'll try to be as clear as possible. This all follows on from your previous question here https://scriptinghelpers.org/questions/52406/need-add-in-script-to-prevent-the-walking-sound-upon-standing-on-the-platform-by-the-character

To make this work I have slightly modified the local script that detects if a player is on a platform, created a moving platform manager module script, and modified your moving platform script so that it can be paused/unpaused.

First the local script, still within StarterPlayerScripts. The only real change here is we look for an event created by the platform manager module, then fire this event when the player steps onto/off of the platform.

local player = game.Players.LocalPlayer
local platformEvent = game.ReplicatedStorage:WaitForChild("PlatformEvent")

local platformName = "Platform"

local isOnPlatform = Instance.new("BoolValue")
isOnPlatform.Name = "IsOnPlatform"
isOnPlatform.Value = false
isOnPlatform.Parent = player

local shouldSilenceWalk = Instance.new("BoolValue")
shouldSilenceWalk.Name = "ShouldSilenceWalk"
shouldSilenceWalk.Value = false
shouldSilenceWalk.Parent = player

while wait(0.1) do
    local character = player.Character
    if character then
        local rootPart = character:FindFirstChild("HumanoidRootPart")
        if rootPart then
            local ray = Ray.new(rootPart.Position - Vector3.new(0, 2, 0), Vector3.new(0, -0.5, 0))
            local part, position = workspace:FindPartOnRay(ray, character)
            if part and part.Name == platformName then
                if not player.IsOnPlatform.Value then
                    isOnPlatform.Value = true
                    platformEvent:FireServer(part)
                end
            elseif player.IsOnPlatform.Value then
                isOnPlatform.Value = false
                platformEvent:FireServer()
            end
        end
    end
end

Next up is the PlatformManager ModuleScript, which you will want to have in ServerStorage. This lets us register any moving platforms we have, and will fire a function on the platform script when a player steps onto/off of the platform.

local PlatformManager = {}

local platformEvent = Instance.new("RemoteEvent")
platformEvent.Name = "PlatformEvent"
platformEvent.Parent = game.ReplicatedStorage

local platforms = {}

function PlatformManager:RegisterPlatform(platform, moveFunc)
    platforms[platform] = {}
    platforms[platform].MoveFunc = moveFunc
    platforms[platform].Count = 0
    platforms[platform].isMoving = false
end

function PlatformManager:PlayerOnPlatform(player, platform)
    local movingPlatform = platforms[platform]
    if movingPlatform then
        movingPlatform[player] = platform
        movingPlatform.Count = movingPlatform.Count + 1
        if not movingPlatform.isMoving then
            movingPlatform.isMoving = true
            movingPlatform.MoveFunc(true)
        end
    end
end

function PlatformManager:PlayerOffPlatform(player)
    for _, movingPlatform in pairs(platforms) do
        local platform = movingPlatform[player]
        if platform then
            platforms[platform].Count = platforms[platform].Count - 1
            if platforms[platform].Count <= 0 then
                platforms[platform].isMoving = false
                platforms[platform].MoveFunc(false)
            end
            movingPlatform[player] = nil
        end
    end
end

platformEvent.OnServerEvent:connect(function(player, ...)
    local args = {...}
    if #args > 0 then
        PlatformManager:PlayerOnPlatform(player, args[1])
    else
        PlatformManager:PlayerOffPlatform(player)
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    PlatformManager:PlayerOffPlatform(player)
end)

return PlatformManager

Finally we have the server script that moves the platform. There should be one of these scripts in every moving platform that you have in the game that you want to share this same logic. I have based this code off of the object hierarchy that I assume you have set up from looking at your code, but just for clarity I have taken a screen grab so you can see how I have things: https://ibb.co/d3dM8b

local PlatformManager = require(game.ServerStorage.PlatformManager)
local movingPlatform = script.Parent.Parent
local platform = movingPlatform.Platform 
local bodyPosition = platform.BodyPosition
local bodyGyro = platform.BodyGyro
local start = movingPlatform.Start
local finish = movingPlatform.Finish
local currentDestination = movingPlatform.Start
local canPlatformMove = false

bodyPosition.Position = platform.Position
bodyGyro.CFrame = platform.CFrame
platform.Anchored = false

local moveFunc = function(isPlayerOnPlatform)
    if isPlayerOnPlatform then
        canPlatformMove = true
        bodyPosition.Position = currentDestination.Position
    else
        canPlatformMove = false
        bodyPosition.Position = platform.Position
    end
end

PlatformManager:RegisterPlatform(platform, moveFunc)

while wait() do
    if (platform.Position - currentDestination.Position).magnitude < 0.25 then
        if canPlatformMove then
            if currentDestination == movingPlatform.Finish then
                currentDestination = movingPlatform.Start
            else
                currentDestination = movingPlatform.Finish
            end
            if canPlatformMove then
                bodyPosition.Position = currentDestination.Position
            end
        end
    end
end

And that's it. Now a platform wont start moving until someone is standing on it, and will stop again once everyone is off of it or if the only player on it logs off. Hope this helps, and sorry for the delay.

Ad

Answer this question