How do I make a part only change its position around the map?

Hello, how do I make a part (when clicked) randomly change its position only around the map?

1 Like

I don’t know how you have a programmer role but you can make one by setting a vector3’s x y z to randomly like 50, 0, 50 (to prevent it in the floor or the air), anyways after that, add a click detector inside the part, make a script then a function that someone clicked on the part, it would then have it’s position set to the random vector3. part.Position = Vector3.new(math.random(1, 50), 0, math.random(1, 50))

Here’s your answer.

1 Like

I already did that but wont it go over the map?

You can get the programmer title by joining the group

Can you show what your current script is? Since all you need to do is locate the part in the Explorer and change it’s Position property which is a Vector3 value.

local Part = workspace.Part

Part.Position = Vector3.new(100,100,100)
1 Like

Same as his solution, Im just trying to find a way to see if I can ensure it wont

Sorry, not sure what you just asked. But could you show your original script for some context?

local players = game:GetService(“Players”)

players.PlayerAdded:Connect(function(ployer)

local leaderboard = Instance.new("Folder")
leaderboard.Parent = ployer
leaderboard.Name = "leaderstats"

local DoorsClicked = Instance.new("IntValue")
DoorsClicked.Name = "Clicked"
DoorsClicked.Value = 0
DoorsClicked.Parent = leaderboard

local door = game.Workspace.Door

door.ClickDetector.MouseClick:Connect(function()
	
	DoorsClicked.Value = DoorsClicked.Value + 1
	
	local position1 = math.random(1,100)
	local position2 = math.random(1,100)		
	
	door.Position = Vector3.new(position1,8.553,position2)
	
end)

end)

Use this script instead, put the part in the middle of where you want it to start.

local part = script.Parent
local clickDetector = part.ClickDetector

local StartCF = part.CFrame

local Coords = {
	MaxX	= 50;
	MinX	= -50;
	MaxZ	= 50;
	MinZ	= -50;
}

local db = true
clickDetector.MouseClick:Connect(function()
	if db then
		db = false
		part.CFrame = StartCF + Vector3.new(math.random(Coords.MinX,Coords.MaxX),0,math.random(Coords.MinZ,Coords.MaxZ))
		wait(.1)
		db = true
	end
end)
2 Likes

Note, you can put this part at 1.000.000.000,0,0 and it would teleport around that position like a rectangle.

1 Like

I took the script you originally had and fixed it up. I tested it and it worked as intended. But @KJry_s’s method is favorable since it makes for easier customization.

I believe your issue may have been having your ClickDetector event connection inside the PlayerAdded event connection. So all I did was seperate the two and that seemed to have worked.

local players = game:GetService("Players")
local door = game.Workspace:WaitForChild("Door")

players.PlayerAdded:Connect(function(player)

	local leaderboard = Instance.new("Folder")
	leaderboard.Parent = player
	leaderboard.Name = "leaderstats"

	local DoorsClicked = Instance.new("IntValue")
	DoorsClicked.Name = "Clicked"
	DoorsClicked.Value = 0
	DoorsClicked.Parent = leaderboard
end)

door.ClickDetector.MouseClick:Connect(function(playerWhoClicked)

		playerWhoClicked.leaderstats.Clicked.Value += 1

		local position1 = math.random(1,100)
		local position2 = math.random(1,100)		

		door.Position = Vector3.new(position1,8.553,position2)

end)
1 Like

Instead of what you wrote I suggest changing it to this:

local players = game:GetService("Players")

local door = game.Workspace.Door

local StartCF = door.CFrame
local db = true -- So you can't spam it.

local Settings = {
	Step 	= .1;
	MaxX	= 5;
	MinX	= -5;
	MaxZ	= 5;
	MinZ	= -5;
}

players.PlayerAdded:Connect(function(player)
	local leaderboard = Instance.new("Folder")
	leaderboard.Parent = player
	leaderboard.Name = "leaderstats"

	local DoorsClicked = Instance.new("IntValue")
	DoorsClicked.Name = "Clicked"
	DoorsClicked.Value = 0
	DoorsClicked.Parent = leaderboard
end)

door.ClickDetector.MouseClick:Connect(function(player)
	if db then
		db = false
		part.CFrame = StartCF + Vector3.new(math.random(Settings.MinX / Settings.Step, Settings.MaxX / Settings.Step),0,math.random(Settings.MinZ / Settings.Step, Settings.MaxZ / Settings.Step))
		player.leaderstats.Clicked.Value += 1
		wait(.1) -- Edit this to change the time people have to wait to click the button again.
		db = true
	end
end)

As otherwise it would give everyone an extra point once they clicked the button.

1 Like

Quick edit so, that you can choose the move increment/steps of the door as it only moves 1 stud away etc; check above for the edited script! :smiley:

1 Like