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

How to make platform move when button is pushed?

Asked by 2 years ago
Edited 2 years ago

im a rookie at coding in Roblox, and i wanted to make a button that makes a platform appear, but it down not work. here is the code i made, and it immediatly activates when you start the game because the button part is touching another part.

local z = 0
local part = script.Parent
local button = workspace.Button1.Button.scriptpart
local e = 1
repeat wait() until button.Touched
    while z < 8 do  
            part.CFrame = CFrame.new(9,8,part.CFrame.Z + 1)
            z = z + 1
        print("z")
        end 

2 answers

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

First of all, you should probably make variables’ names more than just one letter, it will be pretty hard to figure out what they mean later on.

Now, instead of waiting until the button is touched, you can connect a .Touched event to it. This means what whenever the button is touched, the event will fire. The event can fire multiple times, which means loops are not needed.

Then we check if the thing that touched the button is actually a player. Check if the part’s parent touching it has a Humanoid. However only do that if your game does not contain AIs with humanoid, such as zombies. This is done with :FindFirstChild(), it returns nil if the instance does not exist.

If your game does contain things with humanoid other than the player, simply check if the part’s parent’s name is in the player list.

Another thing is, you can probably use Mover Constraints / Body Movers to move your platform, however I’m not really good with those so I won’t try to explain those, instead I’ll just link the articles about them.

You could use TweenService instead, but keep in mind that the player will slide off the platform when the tween is going, this does not occur with Mover Constraints / Body Movers.

--Method 1: Check for humanoid
button.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
        --code
    end
end)


--Method 2:Check for player’s name
local Players = game:GetService('Players')

button.Touched:Connect(function(hit)
    if Players:FindFirstChild(hit.Parent.Name) then
        --code
    end
end)

Mover Constraints: https://developer.roblox.com/en-us/articles/constraint-movers BodyMover: https://developer.roblox.com/en-us/api-reference/class/BodyMover ^This one is deprecated though, so Mover Constraints is preferred

0
thank you, when i put in your code it works now. MrBucketOfficial 39 — 2y
0
If it solved your problem, make sure to mark it as the solution. Plus, you should also take a look at the other answer, they offered some great advise as well SuperLittleAdmin 257 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

Alright theres a few things that need to be changed here:

  1. I could be wrong on this but you can't repeat wait() until button.Touched, I could be wrong but to be safe we will instead connect to the Touched event.

  2. Instead of making a variable and adding to it every time you move the part, we can instead run a for loop which to explain simply is a loop that will run a specific amount of times, unlike a while loop which will run until a condition isn't met.

  3. You mention that the script runs immediately because its touching another part, I think it may just be running immediately because your repeat loop doesn't work however I added a line to check for a humanoid to prevent this just in case.

Your script now looks like this:

local z = 7
local part = script.Parent
local button = workspace.Button1.Button.scriptpart
local e = 1
local canUse = true

button.Touched:Connect(function(hit)
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if canUse and hum then
        canUse = false
        for i = 1, z do
            part.CFrame = part.CFrame * CFrame.new(0, 0, 1)
            --[[another way you can write this that I didn't use so I didn't confuse you is like this:
                 part.CFrame *= CFrame.new(0, 0, 1)]]
           -- it accomplishes the same thing but is faster to type
            wait()
        end
    end
    wait(--[[insert a custom amount of time you want to wait before you can use it again]])
    canUse = true
end)

I know I changed quite a bit so if you have any questions or if the code ends up not working, leave a comment with the question or the errors in your output.

Answer this question