Tweening Range doesnt work

Hello I am creating a Tower Defense game and I have encountered an error that does not show me the Range of the tower and I do not know how to lower it will have an error and my script

image
image

local Players = game:GetService("Players")
local PhysicsService = game:GetService("PhysicsService")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

local UserInputService = game:GetService("UserInputService")

local modules = ReplicatedStorage:WaitForChild("Modules")
local health = require(modules:WaitForChild("Health"))

local gold = Players.LocalPlayer:WaitForChild("Gold")
local towers = ReplicatedStorage:WaitForChild("Towers")

local functions = ReplicatedStorage:WaitForChild("Functions")
local requestTowerFunction = functions:WaitForChild("RequestTower")
local spawnTowerFunction = functions:WaitForChild("SpawnTower")
local sellTowerFunction = functions:WaitForChild("SellTower")
local changeModeFunction = functions:WaitForChild("ChangeTowerMode")

local camera = workspace.CurrentCamera
local gui = script.Parent
local map = workspace:WaitForChild("BasePlate")
local base = map:WaitForChild("Base")
local info = workspace:WaitForChild("Info")

local hoveredInstance = nil
local selectedTower = nil
local towerToSpawn = nil
local canPlace = false
local rotation = 0
local placedTowers = 0
local maxTowers = 100


local function SetupGui()
	health.Setup(base, gui.Info.Health)

	workspace.Mobs.ChildAdded:Connect(function(mob)
		health.Setup(mob)
	end)

	info.Message.Changed:Connect(function(change)
		gui.Info.Message.Text = change
		if change == "" then
			gui.Info.Message.Visible = false
		else
			gui.Info.Message.Visible = true
		end
	end)

	info.Wave.Changed:Connect(function(change)
		gui.Info.Stats.Wave.Text = "Wave:" .. change
	end)

	gold.Changed:Connect(function(change)
		gui.Info.Stats.Gold.Text = "$" .. gold.Value
	end)
	gui.Info.Stats.Gold.Text = "$" .. gold.Value
end

SetupGui()


local function MouseRaycast(model)
	local mousePosition = UserInputService:GetMouseLocation()
	local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
	local raycastParams = RaycastParams.new()

	local blacklist = camera:GetChildren()
	table.insert(blacklist, model)
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = blacklist

	local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)

	return raycastResult
end

local function CreateRangeCircle(tower, placeholder)

	local range = tower.Config.Range.Value
	local height = (tower.PrimaryPart.Size.Y * 1.5) + tower.Humanoid.HipHeight
	local offset = CFrame.new(0, -height, 0)

	local p = Instance.new("Part")
	p.Name = "Range"
	p.Shape = Enum.PartType.Cylinder
	p.Material = Enum.Material.SmoothPlastic
	p.Transparency = 0.8
	p.Color = Color3.new(0, 0.8, 1)
	p.Size = Vector3.new(0, 0, 0)
	p.TopSurface = Enum.SurfaceType.Smooth
	p.BottomSurface = Enum.SurfaceType.Smooth
	p.CFrame = tower.PrimaryPart.CFrame * offset * CFrame.Angles(0, 0, math.rad(90))
	p.CanCollide = false
	p.Anchored = true
	p.Parent = workspace.Camera
	local h = Instance.new("Highlight")
	h.DepthMode = Enum.HighlightDepthMode.Occluded
	h.FillTransparency = 1
	h.OutlineColor = Color3.new(1,1,1)
	h.OutlineTransparency = 0
	h.Parent = p

	if placeholder then
		p.Anchored = false
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = p
		weld.Part1 = tower.PrimaryPart
		weld.Parent = p
		p.Parent = tower
	else
		p.Anchored = true
		p.Parent = workspace.Camera
	end

end

function TweenRangeCircle(tower, placeholder)
	local range = tower.Config.Range.Value
	local cameraRangeCirle = workspace.Camera:FindFirstChild("Range")
	local towerRangeCircle = tower:FindFirstChild("Range")
	
	if cameraRangeCirle then
		cameraRangeCirle.Anchored = true
		local rangeTween = TweenService:Create(cameraRangeCirle, TweenInfo.new(.35, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {Size = Vector3.new(.3, range * 2, range * 2)})
		rangeTween:Play()
	end
	if placeholder then
		towerRangeCircle.Anchored = false
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = towerRangeCircle
		weld.Part1 = tower.PrimaryPart
		weld.Parent = towerRangeCircle
		local rangeTween = TweenService:Create(cameraRangeCirle, TweenInfo.new(.35, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {Size = Vector3.new(.3, range * 2, range * 2)})
	end
end

local function RemovePlaceholderTower()
	if towerToSpawn then
		towerToSpawn:Destroy()
		towerToSpawn = nil
		rotation = 0
	end
end

local function AddPlaceholderTower(name)

	local towerExists = towers:FindFirstChild(name)
	if towerExists then
		RemovePlaceholderTower()
		towerToSpawn = towerExists:Clone()
		towerToSpawn.Parent = workspace

		CreateRangeCircle(towerToSpawn, true)
		TweenRangeCircle(towerToSpawn, true)		

		for i, object in ipairs(towerToSpawn:GetDescendants()) do
			if object:IsA("BasePart") then
				PhysicsService:SetPartCollisionGroup(object, "Tower")
				if object.Name ~= "Range" then
					object.Material = Enum.Material.ForceField
					object.Transparency = 0.3
				end
			end
		end
	end
end

local function ColorPlaceholderTower(color)
	for i, object in ipairs(towerToSpawn:GetDescendants()) do
		if object:IsA("BasePart") then
			object.Color = color
		end
	end
end

gui.Towers.Title.Text = "Towers: " .. placedTowers .. "/" .. maxTowers
for i, tower in pairs(towers:GetChildren()) do
	if tower:IsA("Model") then
		local button = gui.Towers.Template:Clone()
		local config = tower:WaitForChild("Config")
		button.Name = tower.Name
		button.Image = config.Image.Texture
		button.Visible = true
		button.LayoutOrder = config.Price.Value
		button.Price.Text = config.Price.Value

		button.Parent = gui.Towers

		button.Activated:Connect(function()
			local allowedToSpawn = requestTowerFunction:InvokeServer(tower.Name)
			if allowedToSpawn then
				AddPlaceholderTower(tower.Name)
			end
		end)
	end
end

local function toggleTowerInfo()
	workspace.Camera:ClearAllChildren()
	gui.Towers.Title.Text = placedTowers .. "/" .. maxTowers

	if selectedTower then
		CreateRangeCircle(selectedTower)
		TweenRangeCircle(selectedTower)
		gui.Selection.Visible = true
		local config = selectedTower.Config
		gui.Selection.Stats.Damage.Value.Text = config.Damage.Value
		gui.Selection.Stats.Range.Value.Text = config.Range.Value
		gui.Selection.Stats.Cooldown.Value.Text = config.Cooldown.Value
		gui.Selection.Title.TowerName.Text = selectedTower.Name
		gui.Selection.Title.TowerImage.Image = config.Image.Texture
		gui.Selection.Title.OwnerName.Text = config.Owner.Value .. "'s"

		local modes = {
			["First"] = "rgb(150, 150, 150)",
			["Last"] = "rgb(50, 50, 50)", 
			["Near"] = "rgb(50, 150, 0)", 
			["Strong"] = "rgb(200, 50, 50)", 
			["Weak"] = "rgb(50, 100, 200)"
		}
		local color = modes[config.TargetMode.Value]
		gui.Selection.Action.Target.Title.Text = "Target: <font color=\"" .. color .. "\">" .. config.TargetMode.Value .. "</font>"

		if config.Owner.Value == Players.LocalPlayer.Name then
			gui.Selection.Action.Visible = true

			local upgradeTower = config:FindFirstChild("Upgrade")
			if upgradeTower then
				gui.Selection.Action.Upgrade.Visible = true
				gui.Selection.Action.Upgrade.Title.Text = "Upgrade (" .. upgradeTower.Value.Config.Price.Value .. ")"
			else
				gui.Selection.Action.Upgrade.Visible = false
			end
		else
			gui.Selection.Action.Visible = false
		end

	else
		gui.Selection.Visible = false
	end
end

gui.Selection.Action.Target.Activated:Connect(function()
	if selectedTower then
		local modeChangeSuccess = changeModeFunction:InvokeServer(selectedTower)
		if modeChangeSuccess then
			toggleTowerInfo()
		end
	end
end)

gui.Selection.Action.Upgrade.Activated:Connect(function()
	if selectedTower then
		local upgradeTower = selectedTower.Config.Upgrade.Value
		local upgradeSuccess = spawnTowerFunction:InvokeServer(upgradeTower.Name, selectedTower.PrimaryPart.CFrame, selectedTower)

		if upgradeSuccess then
			selectedTower = upgradeSuccess
			toggleTowerInfo()
		end
	end
end)

gui.Selection.Action.Sell.Activated:Connect(function()
	if selectedTower then
		local soldTower = sellTowerFunction:InvokeServer(selectedTower)

		if soldTower then
			selectedTower = nil
			placedTowers -= 1
			toggleTowerInfo()
		end
	end
end)

UserInputService.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end

	if towerToSpawn then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			if canPlace then
				local placedTower = spawnTowerFunction:InvokeServer(towerToSpawn.Name, towerToSpawn.PrimaryPart.CFrame)
				if placedTower then
					placedTowers += 1
					selectedTower = placedTower
					RemovePlaceholderTower()
					toggleTowerInfo()
				end
			end
		elseif input.KeyCode == Enum.KeyCode.R then
			rotation += 90
		end
	elseif hoveredInstance and input.UserInputType == Enum.UserInputType.MouseButton1 then
		local model = hoveredInstance:FindFirstAncestorOfClass("Model")

		if model and model.Parent == workspace.Towers then
			selectedTower = model
		else
			selectedTower = nil
		end

		toggleTowerInfo()
	end
end)

RunService.RenderStepped:Connect(function()
	local result = MouseRaycast(towerToSpawn)
	if result and result.Instance then
		if towerToSpawn then
			hoveredInstance = nil

			if result.Instance.Parent.Name == "TowerArea" then
				canPlace = true
				ColorPlaceholderTower(Color3.new(0,1,0))
			else
				canPlace = false
				ColorPlaceholderTower(Color3.new(1,0,0))
			end
			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) * CFrame.Angles(0, math.rad(rotation), 0)
			towerToSpawn:SetPrimaryPartCFrame(cframe)
		else
			hoveredInstance = result.Instance
		end
	else
		hoveredInstance = nil
	end
end)
1 Like