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

Random brick help?

Asked by 10 years ago

This makes a brick that randomly goes up and down like the bars in this video. It has been working but if I stand on top of them they will teleport on top of me. To try to fix that, I've limited how far it goes up if someone's touching it, but the script is breaking. There are no errors, but whenever the brick is touched the brick stops. Any ideas why?

touched = false
prevnum = 0
script.Parent.Touched:connect(function()
    touched = true
end)

script.Parent.TouchEnded:connect(function()
    touched = false
end)



while touched == false do
    wait(.05)
    script.Parent.CanCollide = false
    a = math.random(3, 3)
    script.Parent.Size = Vector3.new(script.Parent.Size.X, a, script.Parent.Size.Z)
    script.Parent.Position = Vector3.new(script.Parent.Position.X, (a / 2) , script.Parent.Position.Z)
    prevnum = a
    script.Parent.CanCollide = true
end


while touched == true do
    wait(.05)
    if prevnum == 3 then
        script.Parent.CanCollide = false
        b = math.random(3, 4)
        script.Parent.Size = Vector3.new(script.Parent.Size.X, b, script.Parent.Size.Z)
        script.Parent.Position = Vector3.new(script.Parent.Position.X, (b / 2) , script.Parent.Position.Z)
        print(prevnum)
        prevnum = b
        print(prevnum)
        script.Parent.CanCollide = true
    elseif prevnum == 4 then
        script.Parent.CanCollide = false
        b = math.random(3, 5)
        script.Parent.Size = Vector3.new(script.Parent.Size.X, b, script.Parent.Size.Z)
        script.Parent.Position = Vector3.new(script.Parent.Position.X, (b / 2) , script.Parent.Position.Z)
        prevnum = b
        script.Parent.CanCollide = true
    elseif prevnum == 5 then
        script.Parent.CanCollide = false
        b = math.random(3, 6)
        script.Parent.Size = Vector3.new(script.Parent.Size.X, b, script.Parent.Size.Z)
        script.Parent.Position = Vector3.new(script.Parent.Position.X, (b / 2) , script.Parent.Position.Z)
        prevnum = b
        script.Parent.CanCollide = true
    elseif prevnum == 6 then
        script.Parent.CanCollide = false
        b = math.random(3, 7)
        script.Parent.Size = Vector3.new(script.Parent.Size.X, b, script.Parent.Size.Z)
        script.Parent.Position = Vector3.new(script.Parent.Position.X, (b / 2) , script.Parent.Position.Z)
        prevnum = b
        script.Parent.CanCollide = true
    elseif prevnum == 7 then
        script.Parent.CanCollide = false
        b = math.random(3, 7)
        script.Parent.Size = Vector3.new(script.Parent.Size.X, b, script.Parent.Size.Z)
        script.Parent.Position = Vector3.new(script.Parent.Position.X, (b / 2) , script.Parent.Position.Z)
        prevnum = b
        script.Parent.CanCollide = true
    end
end
1
Instead of script.Parent.Position, use script.Parent.CFrame to avoid that. Position checks for collisions when being set, and will move the part to the nearest point above the defined point where it will fit. CFrame doesn't bother with that. deaththerapy 60 — 10y
0
Vector3 clips, so when you change the Position of a part (Which requires a Vector3 value) and there is a part in the way, the part will get moved on top of the other one. CFrame doesn't clip. Basswobble 165 — 10y

1 answer

Log in to vote
0
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

Like death said in his comment, simply change every instance of script.Parent.Position = Vector3.new to script.Parent.CFrame = CFrame.new to stop it jumping on top of the the Player.

The reason it's stopping, however, is because each of those loops is set to only run once. When the brick gets Touched by any part (you should always do a sanity check to see if a Character touched it, by checking if Game.Players:GetPlayerFromCharacter(hit.Parent) returns anything), the first loop ends and the second begins. When the brick stops being Touched by any part (not just the one that first Touched it), the second loop will end and the script will then end.

Try using just this:

while true do
    wait(.05)
    local a = math.random(3, 3) --local so that the variable is only used within the loop.
    script.Parent.Size = Vector3.new(script.Parent.Size.X, a, script.Parent.Size.Z)
    script.Parent.CFrame = CFrame3.new(script.Parent.Position.X, (a / 2) , script.Parent.Position.Z)
end

(By the way, those bars aren't moving randomly in that video: they each represent the volume of a short band of frequencies of sound being played in the song at that moment. IIRC, the lower pitches are on the left, and the higher on the right.)

0
Thanks so much! CardboardRocks 215 — 10y
Ad

Answer this question