Unable To Change a Table Value

So I’ve been working on a round system lately, and I’ve encountered a script bug, where the table value “spawns” isn’t changing after 1st round. Can anyone find the cause of the problem? Any help is appreciated!

How script should work:

  • Player enters the game

  • Map vote Gui appears and Player votes for any map he wants

  • First round has started and everything works fine

  • round ends and everything typed above runs, but spawns stay the same from the 1st round even when it doesn’t exist in Workspace and the 2nd map isn’t the 1st one

Here’s the part of the script it encounters:

function loadMap(mapName:string)
	
	print(mapName)
	local newMap = maps[mapName]:Clone()
	
	newMap.Parent = workspace
	
	game.ReplicatedStorage.Booleans.Playing.Value = true -- some values for GUI to work like it should
	game.ReplicatedStorage.Booleans.Intermission.Value = false
	
	local spawns = newMap:WaitForChild("Spawns"):GetChildren() --getting spawns table

	
	res.PlayPressed.OnServerEvent:Connect(function(plr)
		for i, spawnPart in pairs(spawns) do --choosing a random spawn
			if plr.Character then
				plr.Character.HumanoidRootPart.CFrame = (spawns[i] and spawns[i].CFrame or spawns[i-#spawns]) + Vector3.new(0, 5, 0)
				
				warn(spawns[i], spawns[i].CFrame, spawns[i-#spawns]) --check all the instances and values
				warn(spawns)
				print(listSpawns)
				print(newMap)
				print("maps class name is:", newMap.ClassName)
				
				print(spawnPart)
				print(spawnPart.ClassName)
				
				
				
			end
			
			print("the second",newMap) --second check
		end
		print("the third",newMap) --third check
		warn(newMap, spawns,plr)

	end)
	print("the fourth",newMap) --fourth check
	
	
	return newMap, spawns
end

There the full script:

local rs = game.ReplicatedStorage

local maps = rs:WaitForChild("Maps")
local res = rs:WaitForChild("RemoteEvents")

local numMapsVoting = 3
local intermissionTime = 8
local voteTime = 15


local plrVotes = {}


function addVote(plr:Player, mapName:string)
	
	plrVotes[plr] = mapName
	res:WaitForChild("Voted"):FireAllClients(plrVotes)
end

function removePlayerVote(plr:Player)
	
	plrVotes[plr] = nil
	res:WaitForChild("Voted"):FireAllClients(plrVotes)
end

function loadMap(mapName:string)
	
	print(mapName)
	local newMap = maps[mapName]:Clone()
	
	newMap.Parent = workspace
	
	game.ReplicatedStorage.Booleans.Playing.Value = true -- some values for GUI to work like it should
	game.ReplicatedStorage.Booleans.Intermission.Value = false
	
	local spawns = newMap:WaitForChild("Spawns"):GetChildren() --getting spawns table

	
	res.PlayPressed.OnServerEvent:Connect(function(plr)
		for i, spawnPart in pairs(spawns) do --choosing a random spawn
			if plr.Character then
				plr.Character.HumanoidRootPart.CFrame = (spawns[i] and spawns[i].CFrame or spawns[i-#spawns]) + Vector3.new(0, 5, 0)
				
				warn(spawns[i], spawns[i].CFrame, spawns[i-#spawns]) --check all the instances and values
				warn(spawns)
				print(listSpawns)
				print(newMap)
				print("maps class name is:", newMap.ClassName)
				
				print(spawnPart)
				print(spawnPart.ClassName)
				
				
				
			end
			
			print("the second",newMap) --second check
		end
		print("the third",newMap) --third check
		warn(newMap, spawns,plr)

	end)
	print("the fourth",newMap) --fourth check
	
	
	return newMap, spawns
end

function removeMap(map:Instance)
	
	map:Destroy()
	warn(map, "has been destroyed")
	
	
	
	for _, plr in pairs(game.Players:GetPlayers()) do
		plr.Character.HumanoidRootPart.CFrame = workspace.Lobby.SpawnLocation.CFrame + Vector3.new(0, 5, 0)
		plr.Character.Humanoid.Health = 0
	end
	listSpawns = {}
	print(listSpawns)
	task.wait(2)
	game.ReplicatedStorage.Booleans.Playing.Value = false
	game.ReplicatedStorage.Booleans.Intermission.Value = true
	
end

function handleRound()
	
	
	for i = 1, 30 do  --ROUND DURATION TIME =======================================================================
		task.wait(1)
		
	end
	
	task.wait(5)
end


res:WaitForChild("Voted").OnServerEvent:Connect(addVote)

game.Players.PlayerRemoving:Connect(removePlayerVote)


while true do
	
	task.wait(intermissionTime)
	
	local mapsToVote = maps:GetChildren()
	
	while #mapsToVote > numMapsVoting do
		table.remove(mapsToVote, math.random(1, #mapsToVote))
	end
	
	plrVotes = {}
	
	res:WaitForChild("VotingBegun"):FireAllClients(mapsToVote)
	
	task.wait(voteTime)
	
	local highestVotedFor = nil
	
	local votes = {}
	for i, map in pairs(mapsToVote) do
		votes[map.Name] = 0
		
		if i == 1 then
			highestVotedFor = map.Name
		end
	end
	
	for plr, vote in pairs(plrVotes) do
		
		if votes[vote] then
			votes[vote] += 1
			
			if votes[highestVotedFor] < votes[vote] then
				highestVotedFor = vote
			end
		end
	end
	
	res:WaitForChild("VotingEnded"):FireAllClients()
	
	
	local newMap = loadMap(highestVotedFor)
	warn(newMap)
	
	handleRound()
	
	removeMap(newMap)
end

The main issue was that the spawns table was being recreated each round but not poperly updated so we need something to keep track of the current map spawn points also you should move playpressed event connection outside of loadmap to prevent multiple connections

local rs = game.ReplicatedStorage
local maps = rs:WaitForChild("Maps")
local res = rs:WaitForChild("RemoteEvents")

local numMapsVoting = 3
local intermissionTime = 8
local voteTime = 15

local plrVotes = {}
local currentSpawns = {}

function addVote(plr:Player, mapName:string)
    plrVotes[plr] = mapName
    res:WaitForChild("Voted"):FireAllClients(plrVotes)
end

function removePlayerVote(plr:Player)
    plrVotes[plr] = nil
    res:WaitForChild("Voted"):FireAllClients(plrVotes)
end

function loadMap(mapName:string)
    print(mapName)
    local newMap = maps[mapName]:Clone()
    newMap.Parent = workspace
    
    game.ReplicatedStorage.Booleans.Playing.Value = true
    game.ReplicatedStorage.Booleans.Intermission.Value = false
    
    currentSpawns = newMap:WaitForChild("Spawns"):GetChildren()
    
    return newMap
end

res.PlayPressed.OnServerEvent:Connect(function(plr)
    if #currentSpawns > 0 then
        local randomSpawn = currentSpawns[math.random(1, #currentSpawns)]
        if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") then
            plr.Character.HumanoidRootPart.CFrame = randomSpawn.CFrame + Vector3.new(0, 5, 0)
        end
    end
end)

function removeMap(map:Instance)
    map:Destroy()
    warn(map, "has been destroyed")
    
    currentSpawns = {}
    
    for _, plr in pairs(game.Players:GetPlayers()) do
        if plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") then
            plr.Character.HumanoidRootPart.CFrame = workspace.Lobby.SpawnLocation.CFrame + Vector3.new(0, 5, 0)
            plr.Character.Humanoid.Health = 0
        end
    end
    
    task.wait(2)
    game.ReplicatedStorage.Booleans.Playing.Value = false
    game.ReplicatedStorage.Booleans.Intermission.Value = true
end

function handleRound()
    for i = 1, 30 do
        task.wait(1)
    end
    task.wait(5)
end

res:WaitForChild("Voted").OnServerEvent:Connect(addVote)
game.Players.PlayerRemoving:Connect(removePlayerVote)

while true do
    task.wait(intermissionTime)
    
    local mapsToVote = maps:GetChildren()
    
    while #mapsToVote > numMapsVoting do
        table.remove(mapsToVote, math.random(1, #mapsToVote))
    end
    
    plrVotes = {}
    
    res:WaitForChild("VotingBegun"):FireAllClients(mapsToVote)
    
    task.wait(voteTime)
    
    local highestVotedFor = nil
    
    local votes = {}
    for i, map in pairs(mapsToVote) do
        votes[map.Name] = 0
        
        if i == 1 then
            highestVotedFor = map.Name
        end
    end
    
    for plr, vote in pairs(plrVotes) do
        if votes[vote] then
            votes[vote] += 1
            
            if votes[highestVotedFor] < votes[vote] then
                highestVotedFor = vote
            end
        end
    end
    
    res:WaitForChild("VotingEnded"):FireAllClients()
    
    local newMap = loadMap(highestVotedFor)
    warn(newMap)
    
    handleRound()
    
    removeMap(newMap)
end
1 Like

thanks man, you saved me half a year of my life! Dang i wish i could help you with something too!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.