Hi,
I’m really struggling with this, appreciate any help.
I have a platform which has 4 directional arrow buttons to move the platform when the player stands on it.
I can move the platform and its children fine but the player stays in place. I want the player to move with the platform. I’ve had various success achieving this where I was able to move the player with the platform but then I couldn’t freely move the player with the usual wasd controls. I still want the player to be able to run/jump while on the platform - just when they are still they should move with the platform as opposed to remaining in the same spot whilst the platform moves without them.
I’ve attached my code as well in the hope it helps.
-- Services
local RunService = game:GetService("RunService")
-- Vars
-- Grouping for this obby stage
local stage = workspace.Stage
-- The grouping containing the moving platform and buttons to move it
local platform = stage.Platform
-- Moving platform
local keyCodeToDirectionMap = {
[Enum.KeyCode.W] = Vector3.new(0, 0, -1),
[Enum.KeyCode.A] = Vector3.new(-1, 0, 0),
[Enum.KeyCode.S] = Vector3.new(0, 0, 1),
[Enum.KeyCode.D] = Vector3.new(1, 0, 0),
}
local keyCodeToIsPressedMap = {
[Enum.KeyCode.W] = false,
[Enum.KeyCode.A] = false,
[Enum.KeyCode.S] = false,
[Enum.KeyCode.D] = false,
}
-- Get the direction(s) to platform should move
local function getDirectionsToProcess()
local directions = {}
for keyCode, isPressed in pairs(keyCodeToIsPressedMap) do
if isPressed then
local direction = keyCodeToDirectionMap[keyCode]
table.insert(directions, direction)
end
end
return directions
end
-- Handle moving the platform and all its children parts
local function managePartMovement(step)
local directions = getDirectionsToProcess()
if #directions == 0 then return end
local targetVelocity = 10 -- Measured in studs per second.
local function moveParts(object)
for _, child in ipairs(object:GetChildren()) do
if child:IsA("BasePart") then
local targetPosition = child.CFrame
for _, direction in ipairs(directions) do
local offset = (targetVelocity * step) * direction
targetPosition = targetPosition + offset
end
child.CFrame = targetPosition
end
moveParts(child)
end
end
moveParts(platform)
end
RunService.Heartbeat:Connect(managePartMovement)
-- [Move platform] Button pressing
local buttons = {
platform.backButton,
platform.forwardButton,
platform.leftButton,
platform.rightButton
}
for _, button in pairs(buttons) do
local keyCodeStr = button:GetAttribute("KeyCode")
local keyCode = Enum.KeyCode[string.upper(keyCodeStr)]
local function deselectButton()
keyCodeToIsPressedMap[keyCode] = false
end
local function selectButton()
keyCodeToIsPressedMap[keyCode] = true
end
button.Touched:Connect(function (otherPart)
--[[
Here I handle pressing the four arrow buttons. Calling selectButton() when the player
steps on the button. And calling deselectButton() when they step off the button.
]]--
end)
end
I’ve attempted to read/find other answers using Prismatic Constraint or BodyPosition etc and I could never work it out - get it to work properly. Clearly I’m doing something wrong.
I want my platform to move in the direction of the arrow button(s) being pressed when they are being stood on AND I want the player to move with that same platform.
Thanks so much for reading and any help/code you can provide.