Ello Developers!
As the name says, I've added a round system where every time the round starts, they will be given a random weapon each time they spawn, the problem Is that even If the round starts, the weapons won't be given
Item Giver (Server-Side script located Inside of a folder In workspace, this script gives melees only):
local RS = game:GetService("ReplicatedStorage") local Players = game:GetService("Players") local Folder = RS:WaitForChild("Melee") -- Change the "Folder" if you change the folder name in Replicated Storage local function onCharacterAdded(character) local Tools = Folder:GetChildren() local ToolsTable = Tools local chosenTools = {} repeat local selectedIndex = math.random(1, #ToolsTable) table.insert(chosenTools, ToolsTable[selectedIndex]) table.remove(ToolsTable, selectedIndex) until #chosenTools >= 1 -- Numbers of tools going to give local plr = Players:GetPlayerFromCharacter(character) for i,v in pairs(chosenTools) do Folder:FindFirstChild(v.Name):Clone().Parent = plr.Backpack end end local function onAdded(player) player.CharacterAdded:Connect(onCharacterAdded) -- Triggers when the player spawns or respawns end Players.PlayerAdded:Connect(onAdded)
Round Script (ServerScriptServices, Server-side):
local lobbyLocation = game.Workspace.SpawnLocation.Position + Vector3.new(0,3,0) local ReplicatedStorage = game:GetService('ReplicatedStorage') local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent') local function playGame() local timeAmount = 180 local timerText = 'Remaining Time: ' while timeAmount > 0 do timeEvent:FireAllClients(timeAmount, timerText) wait(1) timeAmount -= 1 end end local function playIntermission() local intermission = 10 local timerText = 'Intermission: ' while intermission > 0 do timeEvent:FireAllClients(intermission, timerText) wait(1) intermission -= 1 end end local function resetPlayers() for _, plr in pairs(game.Players:GetChildren()) do plr.Character.HumanoidRootPart.CFrame = CFrame.new(lobbyLocation) local CurrentMapFolder = game.Workspace.CurrentMap:GetChildren() for _, Map in ipairs(CurrentMapFolder) do if Map:IsA("Model") then Map:Destroy() end end local WeaponGiverFolder = game.Workspace.WeaponGivers:GetChildren() for _, Script in ipairs(WeaponGiverFolder) do if Script:IsA("Script") then Script.Enabled = false end end end end local function teleportPlayers() for _, plr in pairs(game.Players:GetChildren()) do local RandomMap = math.random(1,1) if RandomMap == 1 then local OldSFOTHMap = game:GetService("ReplicatedStorage").Maps["Old SFOTH Map"]:Clone() OldSFOTHMap.Parent = game.Workspace end local WeaponGiverFolder = game.Workspace.WeaponGivers:GetChildren() for _, Script in ipairs(WeaponGiverFolder) do if Script:IsA("Script") then Script.Enabled = true end end end end while true do resetPlayers() playIntermission() teleportPlayers() playGame() end
The problem seems to be that the round doesn't kill the players, only teleports them, and that the random giving of a weapon only happens when they die. One way of solving this is to kill all the players before the round starts.
Another, probably better, way of doing this would be to fire a bindable event whenever the round gets reset, and the giver script iterates through all the players and gives them the random item when the event gets fired.
Giver script:
local RS = game:GetService("ReplicatedStorage") local Players = game:GetService("Players") local Folder = RS:WaitForChild("Melee") local roundEvent = RS:WaitForChild('RoundEvent') -- Change the "Folder" if you change the folder name in Replicated Storage local function givePlayer(character) local Tools = Folder:GetChildren() local ToolsTable = Tools local chosenTools = {} repeat local selectedIndex = math.random(1, #ToolsTable) table.insert(chosenTools, ToolsTable[selectedIndex]) table.remove(ToolsTable, selectedIndex) until #chosenTools >= 1 -- Numbers of tools going to give local plr = Players:GetPlayerFromCharacter(character) for i,v in pairs(chosenTools) do Folder:FindFirstChild(v.Name):Clone().Parent = plr.Backpack end end local function onAdded(player) player.CharacterAdded:Connect(givePlayer) -- Triggers when the player spawns or respawns end roundEvent.Event:Connect(function() for _, plr in Players:GetChildren do givePlayer(plr.Character) end end) Players.PlayerAdded:Connect(onAdded)
Round script:
local lobbyLocation = game.Workspace.SpawnLocation.Position + Vector3.new(0,3,0) local ReplicatedStorage = game:GetService('ReplicatedStorage') local timeEvent = ReplicatedStorage:WaitForChild('TimeEvent') local roundEvent = ReplicatedStorage:WaitForChild('RoundEvent') local function playGame() local timeAmount = 180 local timerText = 'Remaining Time: ' while timeAmount > 0 do timeEvent:FireAllClients(timeAmount, timerText) wait(1) timeAmount -= 1 end end local function playIntermission() local intermission = 10 local timerText = 'Intermission: ' while intermission > 0 do timeEvent:FireAllClients(intermission, timerText) wait(1) intermission -= 1 end end local function resetPlayers() for _, plr in pairs(game.Players:GetChildren()) do plr.Character.HumanoidRootPart.CFrame = CFrame.new(lobbyLocation) local CurrentMapFolder = game.Workspace.CurrentMap:GetChildren() for _, Map in ipairs(CurrentMapFolder) do if Map:IsA("Model") then Map:Destroy() end end local WeaponGiverFolder = game.Workspace.WeaponGivers:GetChildren() for _, Script in ipairs(WeaponGiverFolder) do if Script:IsA("Script") then Script.Enabled = false end end end end local function teleportPlayers() for _, plr in pairs(game.Players:GetChildren()) do local RandomMap = math.random(1,1) if RandomMap == 1 then local OldSFOTHMap = game:GetService("ReplicatedStorage").Maps["Old SFOTH Map"]:Clone() OldSFOTHMap.Parent = game.Workspace end local WeaponGiverFolder = game.Workspace.WeaponGivers:GetChildren() for _, Script in ipairs(WeaponGiverFolder) do if Script:IsA("Script") then Script.Enabled = true end end end end while true do resetPlayers() playIntermission() teleportPlayers() roundEvent:Fire() playGame() end