Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why Isn't the round giving players random Items every time they spawn?

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

1 answer

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

6 comments

0

Ello! It appears that It doesn't work, I also found at line 126, that you typed: "roundEvent:Fire("start")". It appears that Its not a valid memeber

0

roundEvent has to be a bindable event in Replicated storage. That is likely the issue you are running into with my code. If you used a different kind of event for roundEvent, like a RemoteEvent, the code will not work.

0

One mistake i did notice with my code is that it should be "roundEvent:Fire()" instead of "roundEvent:Fire("start")".

0

I think It works! Thank you! Oh and also, on the random tool giver part, on line 32, you forgot to put the () After GetChildren

0

Oh and also, I just realized that, the Item giver won't give you Items anymore when you reset

0

Weird. I'm not sure why that would be, because it triggers whenever the player's character gets reset.

This discussion is preserved as it was. New answers, comments, and votes are closed.