-- ================================================
-- Custom Bedwars Server Script
-- Place inside: ServerScriptService
-- ================================================
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- ================================================
-- CREATE BREAK BED REMOTE
-- This is what the LocalScript fires when
-- a player teleports to an enemy bed.
-- ================================================
local BreakBed = Instance.new("RemoteEvent")
BreakBed.Name = "BreakBed"
BreakBed.Parent = ReplicatedStorage
-- ================================================
-- TEAM ASSIGNMENT HELPER
-- Call this from your game logic to assign a team.
-- Example: assignTeam(player, "Red")
-- ================================================
local function assignTeam(player, teamName)
local tv = player:FindFirstChild("TeamColor")
if not tv then
tv = Instance.new("StringValue")
tv.Name = "TeamColor"
tv.Parent = player
end
tv.Value = teamName
end
-- ================================================
-- AUTO ASSIGN TEAMS ON JOIN (simple example)
-- Cycles players through Red, Blue, Green, Yellow.
-- Replace this with your own team logic if needed.
-- ================================================
local TEAMS = { "Red", "Blue", "Green", "Yellow" }
local teamIndex = 0
Players.PlayerAdded:Connect(function(player)
teamIndex = (teamIndex % #TEAMS) + 1
assignTeam(player, TEAMS[teamIndex])
print("[Bedwars] Assigned " .. player.Name .. " to team: " .. TEAMS[teamIndex])
end)
-- ================================================
-- HANDLE BED BREAK REQUEST
-- ================================================
BreakBed.OnServerEvent:Connect(function(player, bed)
-- Validate: bed must exist and be a Model named "Bed"
if not bed or not bed.Parent then return end
if not bed:IsA("Model") or bed.Name ~= "Bed" then return end
-- Team check on server side (never trust the client alone)
local playerTeam = (player:FindFirstChild("TeamColor") and player.TeamColor.Value) or nil
local bedTeam = (bed:FindFirstChild("TeamColor") and bed.TeamColor.Value) or nil
if playerTeam and bedTeam and playerTeam == bedTeam then
-- Player tried to break their own bed — block it
warn("[Bedwars] " .. player.Name .. " tried to break their own bed!")
return
end
-- Distance check (anti-teleport-cheat safety net)
local char = player.Character
if not char then return end
local hrp = char:FindFirstChild("HumanoidRootPart")
local root = bed.PrimaryPart or bed:FindFirstChildWhichIsA("BasePart")
if hrp and root then
local dist = (hrp.Position - root.Position).Magnitude
if dist > 20 then
warn("[Bedwars] " .. player.Name .. " too far from bed (" .. math.floor(dist) .. " studs)")
return
end
end
-- All checks passed — destroy the bed
bed:Destroy()
print("[Bedwars] " .. player.Name .. " broke a bed!")
end)
print("[Bedwars] Server script loaded.")
Comments
No comments yet
Be the first to share your thoughts!