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)
To ensure only one player can own a base at a time, we need to track which bases are currently assigned. We'll introduce a new table ownedBases that maps a base instance to the player who owns it.
Here's the modified script:
local Players = game:GetService("Players")
local BasesFolder = workspace:WaitForChild("bases")
-- Table to remember which base each player got
local playerBase = {}
-- Table to remember which bases are currently owned (BaseInstance -> PlayerInstance)
local ownedBases = {}
-- Function to assign a random base
local function assignRandomBase(player)
-- If the player already owns a base (e.g., due to re-assignment or a quick re-join),
-- free up their old base first to ensure it becomes available for others.
local currentBase = playerBase[player]
if currentBase and ownedBases[currentBase] == player then
ownedBases[currentBase] = nil -- Mark the base as free
-- Clean up the nametag from the previously owned base
local oldNamePart = currentBase:FindFirstChild("Names")
if oldNamePart then
for _, child in ipairs(oldNamePart:GetChildren()) do
if child.Name == "BaseNameTag" then
child:Destroy()
end
end
end
end
local allBases = BasesFolder:GetChildren()
local availableBases = {}
-- Filter for bases that are not currently owned
for _, base in ipairs(allBases) do
if not ownedBases[base] then
table.insert(availableBases, base)
end
end
if #availableBases == 0 then
warn("No available bases found for player:", player.Name, ". Player will not be assigned a base.")
playerBase[player] = nil -- Ensure the player is not linked to any base
return -- No base assigned
end
-- Pick a random available base
local chosen = availableBases[math.random(1, #availableBases)]
playerBase[player] = chosen -- Link player to the chosen base
ownedBases[chosen] = player -- Mark the chosen base as owned by this player
-- Put a BillboardGui on chosen.Names to show the player's name
local namePart = chosen:WaitForChild("Names")
-- Clean up any old tags on the *newly chosen* base (in case it had a tag from a previous owner)
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 (e.g., no bases available), try now.
-- If still no base after this attempt, they will spawn at the default game spawn.
assignRandomBase(player)
base = playerBase[player] -- Re-check if a base was successfully assigned
if not base then
warn("Player", player.Name, "could not be teleported to a base as none were assigned or available.")
return -- Let the player spawn at the default Roblox spawn location
end
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, "in base", base.Name)
-- If respawn part is missing, they'll also just spawn at the default game spawn.
end
end
-- PlayerAdded handler
Players.PlayerAdded:Connect(function(player)
-- 1) Assign a base & tag it (if available)
assignRandomBase(player)
-- 2) Whenever their character spawns, teleport them
player.CharacterAdded:Connect(function(char)
-- Added check if character still exists, as it might be destroyed if player leaves quickly
if char then
local primaryPart = char:WaitForChild("HumanoidRootPart", 5)
if primaryPart then
teleportToBase(player, char)
else
warn("HumanoidRootPart not found for", player.Name, "'s character within 5 seconds.")
end
end
end)
end)
-- Clean up on leave
Players.PlayerRemoving:Connect(function(player)
local base = playerBase[player]
if base then
-- Mark the base as free in the ownedBases table
ownedBases[base] = nil
-- Clean up the nametag from the base
local namePart = base:FindFirstChild("Names")
if namePart then
for _, child in ipairs(namePart:GetChildren()) do
if child.Name == "BaseNameTag" then
child:Destroy()
end
end
end
end
-- Remove the player's entry from the playerBase mapping
playerBase[player] = nil
end)
Key Changes:
ownedBases Table: A new global table ownedBases = {} is introduced. This table stores a reference from a BaseInstance to the PlayerInstance who currently owns it.assignRandomBase Logic:
assignRandomBase function, it now checks if the player already owns a base. If so, it frees that base in ownedBases and removes its BillboardGui tag. This ensures that if a player were somehow reassigned a base, their old base would correctly become available.allBases and populates an availableBases table with only those bases that are not present as keys in ownedBases.availableBases is empty (meaning all bases are currently owned), it prints a warning, ensures playerBase[player] is nil, and returns, meaning the player will spawn at the default Roblox spawn.chosen, it's not only assigned to playerBase[player] but also explicitly marked as ownedBases[chosen] = player.teleportToBase Logic:
if not base then assignRandomBase(player) fallback remains. After this fallback, it re-checks base. If base is still nil, it means assignRandomBase was unable to find or assign a base (likely because none were available), so it warns and returns, allowing the player to spawn at the game's default spawn.PlayerRemoving Logic:
base they owned.ownedBases[base] = nil to mark that base as free and available for other players.BaseNameTag from the base's "Names" part, visually clearing it.This revised script ensures that each base can only be owned by one player at a time, and players are assigned an available base if one exists.