I’m currently following GnomeCode’s tutorial for a tower defense game and have reached the tower placement stage, however I have encountered an issue on the LocalScript intended for, well, spawning it.
In short, it’s not grabbing the tower’s name (“Dummy”, atm a template for something I’ll fill in at a later date) to send through the RemoteEvent SpawnTower
, but rather my name, so instead of progressing as usual and doing what I want it to do, it’s inputting an error because there is obviously no tower named after me.
I am still a colossal amateur in the field of scripting and am interested in taking advice.
local PhysicsService = game:GetService("PhysicsService")
local UserInputService = game:GetService("UserInputService")
local storage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local events = storage:WaitForChild("Events")
local spawntowerevent = events.SpawnTower
local stuff = storage:WaitForChild("MyBuddies")
local camera = workspace.CurrentCamera
local gui = script.Parent
local towertoSpawn = nil
function raymaker(exclude)
local mposition = UserInputService:GetMouseLocation()
local mouseRay = camera:ViewportPointToRay(mposition.X,mposition.Y)
local raycastP = RaycastParams.new()
raycastP.FilterType = Enum.RaycastFilterType.Exclude
raycastP.FilterDescendantsInstances = exclude
local rcastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000,raycastP)
return rcastResult
end
function addtower(name)
local towerExists = stuff:FindFirstChild(name)
if towerExists then
towertoSpawn = towerExists:Clone()
towertoSpawn.Parent = workspace.ActiveTowers
for i, object in ipairs(towertoSpawn:GetDescendants()) do
if object:IsA("BasePart") then
object.Material = Enum.Material.ForceField
end
end
end
end
gui.Maker.Activated:Connect(function(name)
addtower("Dummy")
end)
UserInputService.InputBegan:Connect(function(input, processed)
if processed then
return
end
if towertoSpawn then
if input.UserInputType == Enum.UserInputType.MouseButton1 then
spawntowerevent:FireServer(towertoSpawn.Name,towertoSpawn.PrimaryPart.CFrame)
end
end
end)
RunService.RenderStepped:Connect(function()
if towertoSpawn then
local result = raymaker({towertoSpawn})
if result and result.Instance then
local x = result.Position.X
local y = result.Position.Y + towertoSpawn["Left Leg"].Size.Y + (towertoSpawn.PrimaryPart.Size.Y / 2)
local z = result.Position.Z
local cframe = CFrame.new(x,y,z)
towertoSpawn:SetPrimaryPartCFrame(cframe)
end
end
end)