Attempt to index nil with transparency

since nobody answered my question about how to make a placement system ( How to make a placement system for tower defense? - Help and Feedback / Scripting Support - DevForum | Roblox), i used my old one, it works fine, but it can only filter one thing, btw this is temporary, it obviously wont be in the game, anyways, when i place the tower, it spams: attempt to index nil with transparency.

code:

local tower = game.ReplicatedStorage.Towers.GroundTower
local TowerType = "Ground"
local button = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local placeTypes = game.Workspace.PlaceTypes
local uis = game:GetService("UserInputService")
local placing = false
local invis = nil

function createInvis()
	local Tower = tower:Clone()
	Tower.Transparency = .5
	mouse.TargetFilter = Tower
	Tower.Parent = workspace
	Tower.Position = mouse.hit.p + Vector3.new(0,tower.Size.Y/2,0)
	
	return Tower
end

button.Activated:Connect(function()
	placing = true
	if invis == nil then
		invis = createInvis()
	end
end)

mouse.Move:Connect(function()
	print(invis)
	if placing == false or invis == nil then return end
	invis.Position = mouse.hit.p + Vector3.new(0,tower.Size.Y/2,0)
	
	uis.InputBegan:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			if mouse.Target.Parent == game.Workspace.floorpath then
			elseif mouse.Target.Parent == placeTypes.Ignore then
			elseif mouse.Target.Parent == placeTypes.Cliff then
				if TowerType == "Cliff" then
					invis.Transparency = 0
					invis = nil
					placing = false
				end
			else
				if TowerType == "Ground" then
					invis.Transparency = 0
					invis = nil
					placing = false
				end
			end
		end
		if input.KeyCode == Enum.KeyCode.Q then
			placing = false
		end
	end)
end)
1 Like

Starts to run when invis exists and keeps running even when invis does not exist anymore.

i put an if statement to check if it still exists and if not it returns.

if placing == false or invis == nil then return end

I see you have a print statement in your code. What did it print? Also try to debug this by adding more print statements here and there, such as in the button.Activated event function to check if it even fires.

Is the tower a model? If it is, you can’t use .Transparency on it because it doesn’t have that property. You’d need to run some sort of loop to toggle the transparency of every single part within the model.

1 Like

if the tower is a model you can do:

> for _,v part in pairs(tower:GetDescendants()) do
         if part:IsA("BasePart") then
             part.Transparency = put transparency here
     end
end

replace tower and put transparency here with the transparency and the tower model you want, it should work