How can I transfer players using a value?

Hello Developers! I have recently made a script which needs to transfer the players in match from script A to scriptB. The scriptB needs to get the players in the match and give them 100 Coins Leaderstats. However scriptA has the players in the match.
Here is the part of scriptA which has the players:

local GameTag = Instance.new("BoolValue")
				GameTag.Name = "GameTag"
				GameTag.Parent = player.Character
				
			else
				-- There is no character
				if not player then
					table.remove(plrs,i)
				end
			end
		end
	end
	

	event:Fire()
				print ("EventFired")
	
	
	for i = GameLength,0,-1 do
		
		for x, player in pairs(plrs) do
			if player then
				
				character = player.Character
				
				if not character then
					-- Left the game
					table.remove(plrs,x)
				else
					if character:FindFirstChild("GameTag") then
						-- They are still alive
						print(player.Name.." is still in the game!")
						game.ServerStorage.ThePlayers.Value = plrs
					else
						-- They are dead
						table.remove(plrs,x)
						print(player.Name.." has been removed!")

					end
				end
			else
				table.remove(plrs,x)
				print(player.Name.." has been removed!")
				
			end
		end
		


		    
		
		Status.Value = "There are "..i.." seconds remaining, and "..#plrs.." players left"
		
		
		
		if #plrs == 0 then
			Status.Value = "Monsters have defeated you!"
		break
		 elseif i == 0 then
			Status.Value = "Time's up!"
			break
		end
		
		
		wait(1)
	end
	
	print("End of game")
	
	wait(2)
	
	for i, player in pairs(game.Players:GetPlayers()) do
		character = player.Character
		
		if not character then
			-- Ignore them
		else
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			end
			
			for _, tool in pairs(player.Backpack:GetChildren()) do
				if tool:FindFirstChild("Price") then
					tool:Destroy()
				end
			end
			
			for _, tool in pairs(character:GetChildren()) do
				if tool:FindFirstChild("Price") then
					tool:Destroy()
				end
			end
			
		end

And here is the part of scriptB which needs to find out how many players are in the match:

local Players = game.ServerStorage.ThePlayers.Value
Players.leaderstats.Coins.Value =  Players.leaderstats.Coins.Value + 100

How can I make these scripts communicate with each other?

Notes:

Both scripts are in ServerScriptService
The Players value which scriptB is referring to is in ServerStorage

I get this error in the output:
15:34:18.248 - ServerScriptService.Monster Spawn:16: attempt to index number with ‘leaderstats’

What I think the Problem Is:
Its because ScriptA cant put the players in that value. Is it even possible to put players in a value?
If yes, than I think I put it in the wrong place.
If no, than what method can I use?

Please read the what I think the problem is and the scripts and also please help me. If you are gonna help me I would like you to implement your method into my code, if you can!

1 Like

So are you trying to award the remaining players 100 coins (just to confirm)?

Yes I am.

Sorry I was at the bathroom im ready to answer questions now.

1 Like

and also, when you made the plrs table, did you insert the direct player?

ex:

for _, player in pairs(game.Players:GetPlayers()) do
    table.insert(plrs, player)
end
1 Like

What do you mean direct player?

1 Like

When the minigame starts, you want all people in the table, when someone dies/gets removed from the game you can do table.remove(plrs, player)

I mean like, when creating the plrs table, did you insert the player object (found in game.Players)

1 Like

I think so. I havent made this script just edited it. But I will try to figure that out.

Yes im pretty sure.

for i, player in pairs(game.Players:GetPlayers()) do
		if player then
1 Like

Ok, so this should be pretty easy to do. Basically, at the end of the round, simply loop through the plrs table, and add 100 coins like so:

for _, player in pairs(plrs) do
    if player then
        player.leaderstats.Coins.Value += 100
    end
end

Ok but does this go into scriptB? The reason im asking this is because ScriptB handles the waves so I want the players in the match be rewarded for every wave they complete. The one that has the players in the match is scriptA

Yes it goes into that script, also before the scripts we wrote add a line

local plrs = {}

EDIT: actually idk, you should’ve done them both in 1 script

Oh well I didnt is there any way I can use two scripts for this?

It would be much easier just to use 1 script. That is, unless you used a module script for all the functions (making plrs table, awarding players, etc.)

However, for something like this, I would recommend just using 1 script.

I havent used 1 script but if I have to is there a way I can implement the wave script into the main script?
If yes, please can you do it because Im not very good at programming.
Here is the wave script:

What’s the 2nd script you’ve sent only one

1 Like

Sorry here is the second script:

@Ologist Are you still on? I still need help

1 Like

Guys are you working on this right now? I just need to see if your still on.


-- Define variables

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ServerStorage = game:GetService("ServerStorage")

local MapsFolder = ServerStorage:WaitForChild("Maps")

local Status = ReplicatedStorage:WaitForChild("Status")

local GameLength = 360

local reward = 1

local cash = math.random(1200,3000)

local event = game.ReplicatedStorage.Event

local Waves = game.ServerStorage.Waves

local Players = game.ServerStorage.ThePlayers.Value


local function onEvent()
    wait (0.5)
    local Wave1 = Waves.Wave1:Clone()
    wait (0.5)
    Wave1.Parent = game.workspace
    repeat 
    wait(0.5) 
    until game.Workspace.Wave1.Monsters.Value == 0 
    Players.leaderstats.Coins.Value =  Players.leaderstats.Coins.Value + 100
    game.Workspace.Wave1:Destroy()
    local Wave2 = Waves.Wave2:clone()
    Wave2.Parent = game.workspace
    repeat 
    wait(0.5) 
    until game.Workspace.Wave2.Monsters.Value == 0 
    game.Workspace.Wave2:Destroy()
	local Wave3 = Waves.Wave3:clone()
    script.Parent.Wave = 3
    Wave3.Parent = game.workspace
    repeat 
    wait(0.5) 
    until game.Workspace.Wave3.Monsters.Value == 0 
    game.Workspace.Wave3:Destroy()
	local Wave4 = Waves.Wave4:clone()
    script.Parent.Wave = 4
    Wave4.Parent = game.workspace
    repeat 
    wait(0.5) 
    until game.Workspace.Wave4.Monsters.Value == 0 
    game.Workspace.Wave4:Destroy()
	local Wave5 = Waves.Wave5:clone()
    script.Parent.Wave = 5
    Wave5.Parent = game.workspace
    repeat 
    wait(0.5) 
    until game.Workspace.Wave5.Monsters.Value == 0 
    game.Workspace.Wave5:Destroy()
	local Wave6 = Waves.Wave6:clone()
    script.Parent.Wave = 6
    Wave6.Parent = game.workspace
    repeat 
    wait(0.5) 
    until game.Workspace.Wave6.Monsters.Value == 0 
    game.Workspace.Wave6:Destroy()
	local Wave7 = Waves.Wave7:clone()
    script.Parent.Wave = 7
    Wave7.Parent = game.workspace
    repeat 
    wait(0.5) 
    until game.Workspace.Wave.Monsters.Value == 0 
    game.Workspace.Wave7:Destroy()
	local Wave8 = Waves.Wave8:clone()
    script.Parent.Wave = 8
    Wave8.Parent = game.workspace
    repeat 
    wait(0.5) 
    until game.Workspace.Wave8.Monsters.Value == 0 
    game.Workspace.Wave8:Destroy()
	local Wave9 = Waves.Wave9:clone()
    script.Parent.Wave = 9
    Wave9.Parent = game.workspace
    repeat 
    wait(0.5) 
    until game.Workspace.Wave9.Monsters.Value == 0 
    game.Workspace.Wave9:Destroy()
    local Wave10 = Waves.Wave:clone()
    script.Parent.Wave = 10
    Wave10.Parent = game.workspace
    repeat 
    wait(0.5) 
    until game.Workspace.Wave10.Monsters.Value == 0 
    game.Workspace.Wave10:Destroy()
    script.Parent.Wave = 0
end

-- Game loop

while true do
	
	Status.Value = "Waiting for enough players to start"
	
	repeat wait() until game.Players.NumPlayers >= 2
	
	Status.Value = "Intermission"
	
	wait(20)
	
	local plrs = {}
	
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			table.insert(plrs,player) -- Add each player into plrs table
		end
	end
	
	local AvailableMaps = MapsFolder:GetChildren()

	local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]
	
	Status.Value = ChosenMap.Name.." is the chosen Map"
	
	local ClonedMap = ChosenMap:Clone()
	ClonedMap.Parent = workspace
	
	-- Teleport players to the map
	Status.Value = "Preparing Arena"
	wait (5)
	local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
	
	if not SpawnPoints then
		print("Spawnpoints not found!")		
	end
	
	local AvailableSpawnPoints = SpawnPoints:GetChildren()
	 
	
	
	for i, player in pairs(plrs) do
		if player then
			character = player.Character
			
			if character then
				
				-- Teleport them
				
				character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame + Vector3.new(0,10,0)
				table.remove(AvailableSpawnPoints,1)
				
				
				
				-- Give them a sword and a revolver
				local equipped = game.ServerStorage.PlayerData[player.Name].Equipped
				
				if equipped.Value ~= "" then
					local weapon = game.ServerStorage.Items[equipped.Value]:Clone()
					weapon.Parent = player.Backpack
				else
					local Knife = ServerStorage.Pistol:Clone()
					    Knife.Parent = player:WaitForChild("Backpack")
				end
				
			
				                    
				

				
				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "GameTag"
				GameTag.Parent = player.Character
				
                event.Event:Connect(onEvent)
			else
				-- There is no character
				if not player then
					table.remove(plrs,i)
				end
			end
		end
	end
	

	event:Fire()
				print ("EventFired")
	
	
	for i = GameLength,0,-1 do
		
		for x, player in pairs(plrs) do
			if player then
				
				character = player.Character
				
				if not character then
					-- Left the game
					table.remove(plrs,x)
				else
					if character:FindFirstChild("GameTag") then
						-- They are still alive
						print(player.Name.." is still in the game!")
						game.ServerStorage.ThePlayers.Value = plrs
					else
						-- They are dead
						table.remove(plrs,x)
						print(player.Name.." has been removed!")

					end
				end
			else
				table.remove(plrs,x)
				print(player.Name.." has been removed!")
				
			end
		end
		


		    
		
		Status.Value = "There are "..i.." seconds remaining, and "..#plrs.." players left"
		
		
		
		if #plrs == 0 then
			Status.Value = "Monsters have defeated you!"
		break
		 elseif i == 0 then
			Status.Value = "Time's up!"
			break
		end
		
		
		wait(1)
	end
	
	print("End of game")
	
	wait(2)
	
	for i, player in pairs(game.Players:GetPlayers()) do
		character = player.Character
		
		if not character then
			-- Ignore them
		else
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			end
			
			for _, tool in pairs(player.Backpack:GetChildren()) do
				if tool:FindFirstChild("Price") then
					tool:Destroy()
				end
			end
			
			for _, tool in pairs(character:GetChildren()) do
				if tool:FindFirstChild("Price") then
					tool:Destroy()
				end
			end
			
		end
		
		player:LoadCharacter()
	end

	ClonedMap:Destroy()
	
	Status.Value = "Game ended"
	
	wait(2)
end

Something like this should do it, if something doesn’t work tell me

2 Likes