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)