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

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

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

✓ Accepted answer

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)

2 comments

0

Oh hey, i never knew that. I thought it would be nil. Thank you.

0

ye, next time, print out touchingparts to see if it's actually nil when you're debugging

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