I recently purchased a tank system but it didnt fit my liking 100% so i made some changes to it. Now I have 1 small problem.
(by me i mean chatgpt )
In a local server test, player 1 can spawn a tank just fine but player 2 cannot spawn a tank whilst player 1 is in his tank. Tanks can be spawned fine from both players if all tanks are empty.
Script:
local Gui = script.RespawnGui
local Group = script.Parent.Parent.Parent
local RespPoint = Group.Platform.RespawPoint
local function SpawnNew(plyr, img, size, color, label)
-- Check if the player already has a tank
if plyr:FindFirstChild("HasTank") and plyr.HasTank.Value == true then
warn("Player already has a tank. Cannot spawn another one.")
return -- Exit if the player already has a tank
end
script.Disabled = true
script.Parent.Color = Color3.fromRGB(30, 30, 30)
script.Parent.Parent.Blueprint.Color = Color3.fromRGB(30, 30, 30)
script.Parent.BillboardGui.Enabled = false
script.Parent:SetAttribute("CanRespawn", false)
local newTonk = game.ReplicatedStorage.TankStuff3.Tank:Clone()
newTonk.Parent = Group
newTonk.PrimaryPart = newTonk.RespawPoint
newTonk:SetPrimaryPartCFrame(CFrame.new(RespPoint.Position))
newTonk:SetPrimaryPartCFrame(CFrame.new(newTonk.RespawPoint.Position, newTonk.RespawPoint.Position + RespPoint.CFrame.LookVector) + Vector3.new(0, 2, 0))
newTonk.RespawPoint:Destroy()
-- Assign ownership of the tank to the player
newTonk:SetAttribute("Owner", plyr.UserId)
-- Ensure each player's tank has independent parts
for _, child in pairs(newTonk:GetDescendants()) do
if child:IsA("BasePart") and (child.Parent.Name == "Color" or child.Name == "Color" or child.Name == "DoorHinge" or child.Name == "FixedAttach" or child.Name == "Connector" or child.Name == "DoorPanel" or child.Name == "Launcher" or child.Name == "MGHinge") then
child.Color = color
end
end
newTonk.Chassi.Turret.Structure.EmblemPart.SurfaceGui.ImageLabel.Image = img
newTonk.Chassi.Turret.Structure.EmblemPart.SurfaceGui.ImageLabel.Size = UDim2.new(size, 0, size, 0)
local filteredMessage = game:GetService("Chat"):FilterStringForBroadcast(label, plyr)
newTonk.Chassi.Turret.Structure.LabelPart.SurfaceGui.TextLabel.Text = filteredMessage
-- Set up the tank parts and physics for the player
local Structure = newTonk.Chassi.Structure
local Turret = newTonk.Chassi.Turret
local Wheels = newTonk.Wheels
local Tracks = newTonk.Tracks
-- Unanchor parts specific to the player's tank
wait(1)
for _, child in pairs(Wheels:GetChildren()) do
if child.Name == "WheelSusp." then
child.Connector.Anchored = false
wait(0.01)
end
end
for _, child in pairs(Tracks.TrackLeft:GetChildren()) do
child.Anchored = false
end
for _, child in pairs(Tracks.TrackRight:GetChildren()) do
child.Anchored = false
end
Structure.qPerfectionWeld.Disabled = false
wait(0.5)
Turret.Structure.qPerfectionWeld.Disabled = false
Turret.Cannon.qPerfectionWeld.Disabled = false
print("respawned tank")
-- Track that the player has a tank
if not plyr:FindFirstChild("HasTank") then
local hasTankValue = Instance.new("BoolValue")
hasTankValue.Name = "HasTank"
hasTankValue.Value = true
hasTankValue.Parent = plyr
else
plyr.HasTank.Value = true
end
-- Make the DeleteVehicle button visible when the tank is spawned
plyr.PlayerGui.Interface.DeleteVehicle.Visible = true
-- Connect the delete button to remove the player's tank and related GUI
plyr.PlayerGui.Interface.DeleteVehicle.MouseButton1Click:Connect(function()
-- Ensure only the player's own tank is deleted
for _, tank in pairs(Group:GetChildren()) do
if tank:GetAttribute("Owner") == plyr.UserId then
tank:Destroy()
print("Deleted player's tank")
-- Hide the DeleteVehicle button
plyr.PlayerGui.Interface.DeleteVehicle.Visible = false
-- Destroy all GUI related to the tank system
if plyr.PlayerGui:FindFirstChild("RespawnGui") then
plyr.PlayerGui.RespawnGui:Destroy()
end
-- Set HasTank to false
if plyr:FindFirstChild("HasTank") then
plyr.HasTank.Value = false
end
break
end
end
end)
wait(15)
script.Parent.Color = Color3.fromRGB(162, 158, 147)
script.Parent.Parent.Blueprint.Color = Color3.fromRGB(162, 158, 147)
script.Parent.BillboardGui.Enabled = true
script.Parent:SetAttribute("CanRespawn", true)
script.Disabled = false
end
script.RespawnTankEvent.OnServerEvent:Connect(function(plyr, img, size, color, label)
-- Each player can now spawn their own tank independently
SpawnNew(plyr, img, size, color, label)
end)
function GiveGui(plyr)
local newGui = Gui:Clone()
newGui.Respawner.Value = script.Parent.Parent.Parent
newGui.Parent = plyr.PlayerGui
newGui.MainFrame.GeneralScript.Disabled = false
-- Ensure the GUI is independent for each player
if not plyr:FindFirstChild("HasTankGui") then
local hasTankGuiValue = Instance.new("BoolValue")
hasTankGuiValue.Name = "HasTankGui"
hasTankGuiValue.Value = false
hasTankGuiValue.Parent = plyr
end
end
script.Parent.Parent.ClickDetector.MouseClick:Connect(function(plyr)
GiveGui(plyr)
end)