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

How to see if there is a part at a specific location?

Hey, currently working on a script that would generate parts in different specific locations if a part is pressed. It probably didn't make much sense, so I will just show you the script:

for i,v in pairs(game.Workspace.Blocks:GetChildren()) do
	v.ClickDetector.MouseClick:Connect(function()
		v.Transparency = 0
		v.CanCollide = true
		local newPart = Instance.new("Part")
		Instance.new("ClickDetector",newPart)
		local newPos = Vector3.new(v.Position.X,v.Position.Y,v.Position.Z+2)
		newPart.Position = newPos
		newPart.TopSurface = "Smooth"
		newPart.Size = Vector3.new(4, 2, 2)
		newPart.Transparency = 0.6
		newPart.CanCollide = false
		newPart.Anchored = true
		newPart.Parent = game.Workspace.Blocks
		newPart.ClickDetector.MouseClick:Connect(function()
			newPart.Transparency = 0
			newPart.CanCollide = true
		end)
		
		local newPart = Instance.new("Part")
		Instance.new("ClickDetector",newPart)
		local newPos = Vector3.new(v.Position.X,v.Position.Y+2,v.Position.Z)
		newPart.Position = newPos
		newPart.TopSurface = "Smooth"
		newPart.Size = Vector3.new(4, 2, 2)
		newPart.Transparency = 0.6
		newPart.CanCollide = false
		newPart.Anchored = true
		newPart.Parent = game.Workspace.Blocks
		newPart.ClickDetector.MouseClick:Connect(function()
			newPart.Transparency = 0
			newPart.CanCollide = true
		end)
		
		local newPart = Instance.new("Part")
		Instance.new("ClickDetector",newPart)
		local newPos = Vector3.new(v.Position.X-4,v.Position.Y,v.Position.Z)
		newPart.Position = newPos
		newPart.TopSurface = "Smooth"
		newPart.Size = Vector3.new(4, 2, 2)
		newPart.Transparency = 0.6
		newPart.CanCollide = false
		newPart.Anchored = true
		newPart.Parent = game.Workspace.Blocks
		newPart.ClickDetector.MouseClick:Connect(function()
			newPart.Transparency = 0
			newPart.CanCollide = true
		end)
	end)
end

Basically, very easy, but I want to make it so the script wouldn't generate a part, if there is already a part in that location. Any ideas?

1 comment

0

I would recommend using math.random to generate a random position

3 answers

I didn't remake your script but I can help you with checking if there is a part in that location

function CheckPos(pos)
	for _,x in pairs(game.Workspace.Blocks:GetChildren()) do
		if x:IsA("Part") then
			if x.Position == pos then
				return true --Block is there
			else
				return false --No block there
			end
		end
	end
end
if YOUR_PART.Position ~= Vector3.new(POSITION) then
	--create part
else
	--there is already a part in that position
end

2 comments

0

He isnt asking for a specific part, he is talking if there are ANY parts in that location.

0

Ah, then i'd use Region3.

local mathX = math.random(100, 200) -- the two numbers are the position you want to generate between

local mathZ = math.random(100, 200)

newPos = Vector3.new(mathX, 200, mathZ)

  • This could work

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