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

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

Asked by 4 years ago

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?

0
I would recommend using math.random to generate a random position robert19735 17 — 4y

3 answers

Log in to vote
0
Answered by 4 years ago

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
Ad
Log in to vote
0
Answered by
AspectW 431 Moderation Voter
4 years ago
if YOUR_PART.Position ~= Vector3.new(POSITION) then
    --create part
else
    --there is already a part in that position
end
0
He isnt asking for a specific part, he is talking if there are ANY parts in that location. BradNewTypical 232 — 4y
0
Ah, then i'd use Region3. AspectW 431 — 4y
Log in to vote
0
Answered by 4 years ago

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

Answer this question