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

Why won't my placement system work? It won't place anything

Asked by
BashGuy10 384 Moderation Voter
5 years ago

Alright, so since the release of miners haven code, i wanted to redo my sandbox tycoon system(because, 100 lines of code, i can now do in 75 lines.). I'm trying to block placement that is colliding with other parts, BUT i don't really know how to use :GetTouchingParts.

I've tried using it, but i dun know how to use it:

mah code

client crap and stuff

local player = game.Players.LocalPlayer
    local mouse = player:GetMouse()

local PosX 
local PosY 
local PosZ 

local model1= game.ReplicatedStorage.Items.Test

local model = model1:Clone()
    model.Parent = workspace

local gridsize = 2

local touchingparts

local function snap()
    PosX = math.floor(mouse.Hit.X / gridsize + 0.5) * gridsize 
    PosZ = math.floor(mouse.Hit.Z / gridsize + 0.5) * gridsize 
    PosY = model.PrimaryPart.Position.Y 
end

mouse.Move:Connect(function()
    mouse.TargetFilter = model
    snap()
    for i, children in pairs(model:GetChildren()) do
        if children:IsA("BasePart") then
            children.Transparency = 0.5
            children.CanCollide = false 
        end
    end
    model:SetPrimaryPartCFrame(CFrame.new(PosX, PosY, PosZ))
end)


mouse.Button1Down:Connect(function()
    touchingparts = model.PrimaryPart:GetTouchingParts()

    if not touchingparts then
        game.ReplicatedStorage.Remotes.PlaceObj:FireServer(PosX, PosY, PosZ, model1)
    end
end)

server script(i've removed leaderstats from this script)


local rp = game:GetService("ReplicatedStorage") local items = rp.Items local modules = rp.Modules local remotes = rp.Remotes remotes.PlaceObj.OnServerEvent:Connect(function(plr, posx, posy, posz, model) local newmodel = model:Clone() newmodel:SetPrimaryPartCFrame(CFrame.new(posx, posy, posz)) newmodel.Parent = workspace end)

1 answer

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

There will be a touching parts table even if there are no parts touching the part. Check the number of items in the table instead:

mouse.Button1Down:Connect(function()
    touchingparts = model.PrimaryPart:GetTouchingParts()

    if #touchingparts < 1 then
        print("there are no parts touching, firing remote"0
            game.ReplicatedStorage.Remotes.PlaceObj:FireServer(PosX, PosY, PosZ, model1)
    else
        print("the following parts are touching: ")
        for i,v in ipairs(touchingparts)do
            print(v)
        end
    end
end)
0
Oh hey, i never knew that. I thought it would be nil. Thank you. BashGuy10 384 — 5y
0
ye, next time, print out touchingparts to see if it's actually nil when you're debugging royaltoe 5144 — 5y
Ad

Answer this question