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

Making a part move randomly when a person enters the game?

Asked by 9 years ago

I've recently just started to learn the basics of Lua, however when I run this script, the second function does not seem to work. The first works just fine and destroys when I touch it, but it does not move randomly, as I have tried to attempt in the second function.

Model = game.Workspace.Part
function destroy()
game.Workspace.Part:Destroy()
end
script.Parent.Touched:connect(destroy)

function move()
    Model:MoveTo(math.random())
    wait(2)
    repeat until script.Parent.Touched
end
game.Players.PlayerAdded:connect(move)
0
There's not a whole not right in your second function. I suggest you look up MoveTo on the wiki, as well as reviewing how events work and what repeat loops are used for. Perci1 4988 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

MoveTo is a function that takes a Vector3. math.random is a function that returns a single number. So, MoveTo is expecting 3 numbers, but you're passing 1 into it. Try this:

local Model = game.Workspace.Part

function destroy()
    game.Workspace.Part:Destroy()
end
script.Parent.Touched:connect(destroy)

function move()
    local touched

    Model.Tocuhed:connect(function(hit)
        if hit and hit.Parent:FindFirstChild("Humanoid") then
            touched = true
        end
    end)

    while not touched do
        Model.Position = Vector3.new(math.random(),math.random(),math.random())*10
        wait(1)
    end
end
game.Players.PlayerAdded:connect(move)

Also, the line repeat until script.Parent.Touched does nothing for your code, so I suggest removing that.

EDIT: MoveTo was not even applicable here.

EDIT 2: Made it loop.

0
Sadly, this does not seem to work. I have changed it exactly and the brick does not move. toughjohncena1256 57 — 9y
1
MoveTo() is a Function of a Model which means it won't work if that Function is used on a BasePart. UserOnly20Characters 890 — 9y
0
Yes, UserOnly20Characters is right. I didn't see you're trying to use a part. Let me edit this code for you. iconmaster 301 — 9y
0
Thank you. The part moves when I play solo but to only one place. Is there any way I can repeat it moving randomly until it is touched? toughjohncena1256 57 — 9y
View all comments (2 more)
0
Yes, there is! Let me edit it again. iconmaster 301 — 9y
0
Thank you so much. You were a great help. toughjohncena1256 57 — 9y
Ad

Answer this question