make it so that only one person can own a base at a time --…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
make it so that only one person can own a base at a time -- ServerScriptService/RandomBaseSpawner.lua
local Players = game:GetService("Players") local BasesFolder = workspace:WaitForChild("bases")
-- Table to remember which base each player got local playerBase = {}
-- Function to assign a random base local function assignRandomBase(player) local allBases = BasesFolder:GetChildren() if #allBases == 0 then warn("No bases found in workspace.bases!") return end
-- Pick one
local chosen = allBases[math.random(1, #allBases)]
playerBase[player] = chosen
-- Put a BillboardGui on chosen.Names to show the player's name
local namePart = chosen:WaitForChild("Names")
-- Clean up any old tags (in case you reassign)
for _, child in ipairs(namePart:GetChildren()) do
if child.Name == "BaseNameTag" then
child:Destroy()
end
end
-- Create the tag
local bgui = Instance.new("BillboardGui")
bgui.Name = "BaseNameTag"
bgui.Adornee = namePart
bgui.Size = UDim2.new(0, 200, 0, 50)
bgui.StudsOffset = Vector3.new(0, 2, 0)
bgui.AlwaysOnTop = true
bgui.Parent = namePart
local label = Instance.new("TextLabel")
label.Size = UDim2.new(1, 0, 1, 0)
label.BackgroundTransparency = 1
label.TextScaled = true
label.TextColor3 = Color3.new(1, 1, 1)
label.TextStrokeTransparency = 0.5
label.Text = player.Name .. "'s Base"
label.Parent = bgui
end
-- Function to teleport character to their base's Respawn local function teleportToBase(player, character) local base = playerBase[player] if not base then -- If for some reason they joined but weren't assigned, do it now assignRandomBase(player) base = playerBase[player] end
local respawnPart = base:FindFirstChild("Respawn")
if respawnPart and character.PrimaryPart then
-- Move them slightly above the part so they don't get stuck
local cf = respawnPart.CFrame + Vector3.new(0, respawnPart.Size.Y/2 + 2, 0)
character:SetPrimaryPartCFrame(cf)
else
warn("Couldn't find Respawn part or character.PrimaryPart for", player.Name)
end
end
-- PlayerAdded handler Players.PlayerAdded:Connect(function(player) -- 1) assign a base & tag it assignRandomBase(player)
-- 2) whenever their character spawns, teleport them
player.CharacterAdded:Connect(function(char)
-- wait for a PrimaryPart (usually HumanoidRootPart)
char:WaitForChild("HumanoidRootPart", 5)
teleportToBase(player, char)
end)
end)
-- Clean up on leave Players.PlayerRemoving:Connect(function(player) playerBase[player] = nil end)
Below is a drop-in replacement for ServerScriptService/RandomBaseSpawner.lua that guarantees one-and-only-one owner per base at any moment.
The changes are fully commented, but in short we:
---------------------------------------------------------------------
-- RandomBaseSpawner.lua (One-owner-per-base edition)
---------------------------------------------------------------------
local Players = game:GetService("Players")
local BasesFolder = workspace:WaitForChild("bases")
-- Player ➜ Base
local playerBase = {}
-- Base ➜ Player (the new table that stops double-booking)
local assignedBases = {}
---------------------------------------------------------------------
-- Helper: create / update the billboard on the base
---------------------------------------------------------------------
local function tagBase(base, player)
local namePart = base:WaitForChild("Names")
-- Wipe any previous tag
local old = namePart:FindFirstChild("BaseNameTag")
if old then old:Destroy() end
local bgui = Instance.new("BillboardGui")
bgui.Name = "BaseNameTag"
bgui.Adornee = namePart
bgui.Size = UDim2.new(0, 200, 0, 50)
bgui.StudsOffset = Vector3.new(0, 2, 0)
bgui.AlwaysOnTop = true
bgui.Parent = namePart
local label = Instance.new("TextLabel")
label.Size = UDim2.new(1, 0, 1, 0)
label.BackgroundTransparency = 1
label.TextScaled = true
label.TextColor3 = Color3.new(1, 1, 1)
label.TextStrokeTransparency = 0.5
label.Text = player.Name .. "'s Base"
label.Parent = bgui
end
---------------------------------------------------------------------
-- Assign an unclaimed base to the player
---------------------------------------------------------------------
local function assignRandomBase(player)
-- Build a list of all currently FREE bases
local freeBases = {}
for _, base in ipairs(BasesFolder:GetChildren()) do
if not assignedBases[base] then
table.insert(freeBases, base)
end
end
if #freeBases == 0 then
warn(("All bases are taken! Could not assign one to %s."):format(player.Name))
return -- or queue / kick / reuse – your choice
end
-- Pick a random free base
local chosen = freeBases[math.random(1, #freeBases)]
-- Record the ownership in BOTH tables
playerBase[player] = chosen
assignedBases[chosen] = player
-- Slap on the billboard
tagBase(chosen, player)
end
---------------------------------------------------------------------
-- Teleport the character to their base
---------------------------------------------------------------------
local function teleportToBase(player, character)
local base = playerBase[player]
if not base then
assignRandomBase(player)
base = playerBase[player]
if not base then return end -- still none? bail
end
local respawnPart = base:FindFirstChild("Respawn")
local root = character:FindFirstChild("HumanoidRootPart")
if respawnPart and root then
local cf = respawnPart.CFrame + Vector3.new(0, respawnPart.Size.Y/2 + 2, 0)
character:SetPrimaryPartCFrame(cf)
end
end
---------------------------------------------------------------------
-- Releases a base when the player leaves (or if you ever need to)
---------------------------------------------------------------------
local function releaseBase(player)
local base = playerBase[player]
if base then
-- Remove tag
local namePart = base:FindFirstChild("Names")
if namePart then
local tag = namePart:FindFirstChild("BaseNameTag")
if tag then tag:Destroy() end
end
-- Free it
assignedBases[base] = nil
playerBase[player] = nil
end
end
---------------------------------------------------------------------
-- Player join / leave hooks
---------------------------------------------------------------------
Players.PlayerAdded:Connect(function(player)
assignRandomBase(player)
player.CharacterAdded:Connect(function(char)
char:WaitForChild("HumanoidRootPart", 5)
teleportToBase(player, char)
end)
end)
Players.PlayerRemoving:Connect(releaseBase)
Drop the script in and press Play—each base will be claimed by at most one player, and when that player leaves the base immediately becomes available for the next person.