I keep getting findfirstchild errors, and i feel like im starting to mess up the logic and i hope someone can help me find out what i did wrong because i just cant seem to find it
also im hoping to know how to improve this script since its kinda messy and looks wrong
this is a part of a local script
UserInputService.InputEnded:Connect(function(InputObject, GameProcessed)
if not IsPlacing then
if InputObject.UserInputType == Enum.UserInputType.MouseButton1 and GameProcessed == false then
local RaycastResult = MouseRaycast("Include", {game.Workspace.CurrentUnits})
if RaycastResult and RaycastResult.Instance and RaycastResult.Instance.Parent.Parent == game.Workspace.CurrentUnits then
local Unit = RaycastResult.Instance.Parent
if CurrentUpgradeUnit ~= Unit then
CurrentSelected = true
CurrentUpgradeUnit = Unit
CurrentID = Unit:GetAttribute("ID")
UnitNameLabel.Text = Unit.Name
local Goal = {} Goal.Position = UDim2.fromScale(0.98, 0.5)
local Info = TweenInfo.new(0.25, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out)
local Tween = TweenService:Create(UnitFrame, Info, Goal)
Tween:Play()
if CurrentUpgradeUnit:GetAttribute("Level") ~= CurrentUpgradeUnit:GetAttribute("MaxLevel") then
UpgradeButton.Visible = true
local Upgrading = false
local Connection1 = nil
Connection1 = UpgradeButton.Activated:Connect(function()
if CurrentSelected == true then
if CurrentUpgradeUnit:GetAttribute("ID") == CurrentID and CurrentUpgradeUnit:GetAttribute("Owner") == Player.Name and Upgrading == false then
local UpgradeLevel = CurrentUpgradeUnit:GetAttribute("Level") + 1
if Player.leaderstats.Cash.Value > ReplicatedStorage.Units:FindFirstChild(UpgradeLevel):FindFirstChild(CurrentUpgradeUnit.Name):GetAttribute("Cost") then
Upgrading = true
UpgradeEvent:FireServer(CurrentUpgradeUnit, CurrentID)
local Connection2 = nil
Connection2 = UpgradeEvent.OnClientEvent:Connect(function(CurrentUnit)
Connection2:Disconnect()
Upgrading = false
CurrentUpgradeUnit = CurrentUnit
print(CurrentUnit:GetAttribute("Level"), CurrentUpgradeUnit:GetAttribute("MaxLevel"))
if CurrentUnit:GetAttribute("Level") == CurrentUpgradeUnit:GetAttribute("MaxLevel") then
UpgradeButton.Visible = false
else
UpgradeButton.Visible = true
end
repeat
task.wait()
until CurrentSelected == false
Connection1:Disconnect()
end)
end
end
end
end)
end
end
else
local Goal = {} Goal.Position = UDim2.fromScale(1.28, 0.5)
local Info = TweenInfo.new(0.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.In)
local Tween = TweenService:Create(UnitFrame, Info, Goal)
Tween:Play()
CurrentSelected = false
CurrentUpgradeUnit = nil
end
end
end
end)
the serverscript
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local System = require(ServerScriptService.SystemModule)
-- Remote Event
local DeployEvent = ReplicatedStorage.DeployEvent
local UpgradeEvent = ReplicatedStorage.UpgradeEvent
local function EnemyKilled(Enemy)
wait(0.5)
Enemy:Destroy()
end
DeployEvent.OnServerEvent:Connect(function(Player, UnitName, UnitPosition, UnitLowestPosition, ID)
local Unit = ReplicatedStorage.Units:FindFirstChild(0):FindFirstChild(UnitName)
if Player.leaderstats.Cash.Value >= Unit:GetAttribute("Cost") then
Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - Unit:GetAttribute("Cost")
Unit = Unit:Clone()
Unit.Parent = game.Workspace.CurrentUnits
Unit:SetPrimaryPartCFrame(UnitPosition)
Unit:SetAttribute("Owner", Player.Name)
Unit:SetAttribute("ID", ID)
local Space = ReplicatedStorage.Assets.Space:Clone()
Space.Parent = game.Workspace.CurrentAssets
Space.Position = UnitLowestPosition
Space.Size = Vector3.new(0.001, Unit:GetAttribute("Space") * 2, Unit:GetAttribute("Space") * 2)
Space.Anchored = true
for Quantity, Object in pairs(Unit:GetDescendants()) do
if Object:IsA("BasePart") then
Object.Anchored = true
Object.CollisionGroup = "Units"
end
end
UpgradeEvent.OnServerEvent:Connect(function(Player, UpgradeUnit, CurrentID)
task.wait()
if UpgradeUnit then
if Unit:GetAttribute("ID") == CurrentID and UpgradeUnit:GetAttribute("Owner") == Player.Name then
local UpgradeLevel = Unit:GetAttribute("Level") + 1
local UpgradeUnit = ReplicatedStorage.Units:FindFirstChild(UpgradeLevel):FindFirstChild(UnitName)
if UpgradeUnit and Player.leaderstats.Cash.Value >= UpgradeUnit:GetAttribute("Cost")and UpgradeUnit:FindFirstChild("Humanoid").Health > 0 then
Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value - UpgradeUnit:GetAttribute("Cost")
Unit:Destroy()
Unit = ReplicatedStorage.Units:FindFirstChild(UpgradeLevel):FindFirstChild(UnitName):Clone()
Unit.Parent = game.Workspace.CurrentUnits
Unit:SetPrimaryPartCFrame(UnitPosition)
Unit:SetAttribute("Owner", Player.Name)
Unit:SetAttribute("ID", CurrentID)
for Quantity, Object in pairs(Unit:GetDescendants()) do
if Object:IsA("BasePart") then
Object.Anchored = true
Object.CollisionGroup = "Units"
end
end
UpgradeEvent:FireClient(Player, Unit)
end
end
end
end)
while Unit and Unit.Parent do
if Unit:GetAttribute("AttackType") == "Shooting" then
local TargetedEnemy = System.Detect(Unit, Unit:GetAttribute("TargetType"))
if TargetedEnemy then
local EnemyHumanoid = TargetedEnemy:FindFirstChild("Humanoid")
EnemyHumanoid.Health = EnemyHumanoid.Health - Unit:GetAttribute("Damage")
local Goal = {} Goal.CFrame = CFrame.new(Unit.PrimaryPart.Position, Vector3.new(TargetedEnemy.PrimaryPart.Position.X, Unit.PrimaryPart.Position.Y, TargetedEnemy.PrimaryPart.Position.Z))
local Info = TweenInfo.new(0.25, Enum.EasingStyle.Exponential)
local Tween = TweenService:Create(Unit.PrimaryPart, Info, Goal)
for Quantity, Object in pairs(Unit:GetDescendants()) do
if Object:IsA("BasePart") then
Object.Anchored = false
end
end
Tween:Play()
Tween.Completed:Wait()
for Quantity, Object in pairs(Unit:GetDescendants()) do
if Object:IsA("BasePart") then
Object.Anchored = true
end
end
if EnemyHumanoid.Health <= 0 then
EnemyKilled(TargetedEnemy)
end
task.wait(Unit:GetAttribute("Cooldown") - 0.25)
end
end
task.wait()
end
Space:Destroy()
end
end)