Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

1 comment

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.

1 answer

✓ Accepted answer

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.

6 comments

0

Sadly, this does not seem to work. I have changed it exactly and the brick does not move.

1

MoveTo() is a Function of a Model which means it won't work if that Function is used on a BasePart.

0

Yes, UserOnly20Characters is right. I didn't see you're trying to use a part. Let me edit this code for you.

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?

0

Yes, there is! Let me edit it again.

0

Thank you so much. You were a great help.

This discussion is preserved as it was. New answers, comments, and votes are closed.