GetTouchingParts error

It might be a silly question, but basically, I’ve built a placement system for my game, and the results are pretty good, but there is one problem: game.Workspace.BuildParts.Barrel. Script:15: attempt to index nil with "GetTouchingParts'. this is a simple script that checks if any part is touching it or if there is any other part that has the same name as this part but the results are Workspace.BuildParts.Barrel.Script:15: attempt to index nil with 'GetTouchingParts'

Any help?

Ah, forgot the code:

if script.Parent.Name == "Barrel" then
	for i,v in pairs(game.Workspace.BuildParts:GetChildren()) do
		if v.Name == "Barrel" and v.Owner.Value == script.Parent.Owner.Value then
			script.Parent:Destroy()
		end
	end
elseif script.Parent.Name == "Handle" then
	for i,v in pairs(game.Workspace.BuildParts:GetChildren()) do
		if v.Name == "Handle" and v.Owner.Value == script.Parent.Owner.Value then
			script.Parent:Destroy()
		end
	end
end

local parts = script.Parent:GetTouchingParts()
if #parts == 0 then
	for i, v in pairs(parts) do
		if v.Name ~= "Workshop1" and v.Name ~= "Invisible" then
			if v.Owner.Value == script.Parent.Owner.Value then
				script.Parent:Destroy()
			end
		end
	end
end

Try printing inside that scriot script.Parent. Does it happen all the time?

You already destroyed script.Parent lol

I’m not sure if you meant to do v.Parent:Destroy() or if instead you want to just disable the script entirely after each of your “scenarios” for why script.Parent should get destroyed so there’s no conflict for if it’s already destroyed (or an “if script.Parent then”)

It’s for my game, a silly way (I guess) to check if the part is colliding with another part but uhh its hard to explain, so um yeah.

Everything… Seems correct, except for the first reason, those other reasons are hard to explain I basically use the same position for camera to build and the build is client sided

The reason I did this is to prevent exploiters, and it would be hard to handle players with multiple camera positions.

I hope you understood! But I’ll look into it!

EDIT: I know client sided builds aren’t that secure but as far as I know the way that I built it is secure.

You just need to make sure it can’t index nil, meaning it has to exist. All you have to do is check if it exists before running it with an if statement?

To fix the error, wouldn’t something like this work:

if script.Parent.Name == "Barrel" then
	for i,v in pairs(game.Workspace.BuildParts:GetChildren()) do
		if v.Name == "Barrel" and v.Owner.Value == script.Parent.Owner.Value then
			script.Parent:Destroy()
		end
	end
elseif script.Parent.Name == "Handle" then
	for i,v in pairs(game.Workspace.BuildParts:GetChildren()) do
		if v.Name == "Handle" and v.Owner.Value == script.Parent.Owner.Value then
			script.Parent:Destroy()
		end
	end
end
if script.Parent then 
    local parts = script.Parent:GetTouchingParts()
    if #parts == 0 then
	    for i, v in pairs(parts) do
		    if v.Name ~= "Workshop1" and v.Name ~= "Invisible" then
		    	if v.Owner.Value == script.Parent.Owner.Value then
		    		script.Parent:Destroy()
		    	end
	    	end
	    end
    end
end 
1 Like

It still doesn’t work
Nothing is getting placed
Probably something is placed and instantly removed.