Value becoming nil for no reason

Hey!
I am making a placement system and it worked before, but now it just doesn’t work. This is (what I suspect) a very vital value going nil for no reason. There is only 2 times its used in the script, one to define it, and one to change it to a object. Now, before you tell me the object doesn’t exist, the script checks if it does multiple times and it returns it. Then when it’s called out of no where, it goes nil.

--Local script
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PhysicsService = game:GetService("PhysicsService")

local getMap = ReplicatedStorage.SelectedMap
if not getMap.Value then
	getMap:GetPropertyChangedSignal("Value"):Wait()
end

local getReplicatedMap = ReplicatedStorage.Maps:FindFirstChild(tostring(getMap.Value))
if not getReplicatedMap then
	error("REPLICATED MAP: "..getMap.Name.." DOES NOT EXIST")
end

local PlacementEvents = ReplicatedStorage:WaitForChild("PlacementEvents")
local PlacementEventsMain = PlacementEvents:WaitForChild("Main")
local spawnTowerEvent = PlacementEventsMain:WaitForChild("SpawnTower")


local Camera = workspace.Camera
local gui = script.Parent
local TowerToSpawn = nil

local function MouseRaycast(blacklist)
	local mousePosition = UserInputService:GetMouseLocation()
	
	local mouseRay = Camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = blacklist
	
	local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)
	
	return raycastResult
end

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then
		return
	end
	if TowerToSpawn then 
		print("PA")
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			print("EA")
			spawnTowerEvent:FireServer(TowerToSpawn.Name, TowerToSpawn.PrimaryPart.CFrame)
		else
			warn("uh oh")
		end
	else
		warn("Ap")
	end
end)

local function AddPlaceHolderTower(name : string)
	local towerExists = getReplicatedMap.Towers:FindFirstChild(tostring(name))
	if towerExists then
		towerToSpawn = towerExists:Clone()
		towerToSpawn.Parent = workspace.Towers.PlacingTowers
		
		for i, object in ipairs(towerToSpawn:GetDescendants()) do
			if object:IsA("BasePart") then
				PhysicsService:SetPartCollisionGroup(object, "Tower")
				object.Material = Enum.Material.Neon
			end
		end
	end
end

gui["1"].MainButton.Activated:Connect(function()
	AddPlaceHolderTower("Tower")
end)
RunService.RenderStepped:Connect(function()
	if towerToSpawn then
		local result = MouseRaycast({towerToSpawn})
		if result and result.Instance then
			local x = result.Position.X
			local y = result.Position.Y + towerToSpawn.Humanoid.HipHeight + (towerToSpawn.PrimaryPart.Size.Y / 2)
			local z = result.Position.Z


			local cframe = CFrame.new(x,y,z)
			towerToSpawn:SetPrimaryPartCFrame(cframe)
		end
	end
end)

Since your script runs on local script, I felt like it would be better to use :WaitForChild to be exactly sure that the object has successfully replicated to the client.

local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PhysicsService = game:GetService("PhysicsService")

local getMap = ReplicatedStorage:FindFirstChild("SelectedMap")
--if not getMap.Value then
--	getMap:GetPropertyChangedSignal("Value"):Wait()
--end

https://developer.roblox.com/en-us/api-reference/function/Instance/WaitForChild

1 Like

It is used. If you read the script it is used a few times.

This is important. The value here starts at nil, and changes once it gets decided.

So the value is only used once? Could you show me the output of the script?

1 Like

image
The reason there is 26x is because I spammed it

May I ask if towerToSpawn and TowerToSpawn are the same variable?

No. They are both different. (Charrr)

Would it be more convenience if we move this to a private conversation so it doesn’t flooded the chat here?

1 Like

I’m aright to do so. (Charrrrrrrr)

if not getMap.Value then
	getMap:GetPropertyChangedSignal("Value"):Wait()
end

Its value is never being set (at least from the provided script).

Actually this post got solved. First of I misspelt a important variable, then forgot that modules don’t run until required.
To clarify what that is:
It is the map that was voted. Since you need to vote for the map, the value will be nil for 30-60 seconds, so, you need to yield it until the map is ready or you will get punched with Attempted to Index nil error.