Normal script creating 1 instance for each player

Hi! I have a remote event that gets fired when u click a button, then through a server script i loop for each player and check their userId. If their UserId isn’t -1, an Instance is created with their name. The problem is that it creates like 1 instance for each player there is but all with the same name. Also when I then destroy the value created in the same script it deletes both of them. This is my code:

game:GetService("ReplicatedStorage"):WaitForChild("StartGame").OnServerEvent:Connect(function(plr, maxPlrs)
	for i,v in pairs(game:GetService("Players"):GetChildren()) do
		if v.UserId ~= -1 then
			
			local value = Instance.new("BoolValue")
			value.Parent =  game:GetService("ReplicatedStorage"):WaitForChild("PlayersInGame")
			value.Name = v.Name
			value.Value = false
			wait(5)
			value:Destroy()
			
			local slot = game:GetService("Workspace"):WaitForChild("Game"):WaitForChild("Slots"):WaitForChild("Slot"..current)
			local cframe = Vector3.new(slot.Position.X, slot.Position.Y + 5, slot.Position.Z)
			v.Character.HumanoidRootPart.CFrame = CFrame.new(cframe, game:GetService("Workspace"):WaitForChild("Game"):WaitForChild("EndPart").Position)
			v.Character.Humanoid.WalkSpeed = 0
			current += 1
			game:GetService("ReplicatedStorage"):WaitForChild("PlayersPlaying").Value += 1
		else
			local slot = game:GetService("Workspace"):WaitForChild("Game"):WaitForChild("EndPart")
			v.Character.HumanoidRootPart.CFrame = CFrame.new(slot.CFrame.X, slot.CFrame.Y + 5, slot.CFrame.Z)
		end
	end
end)

If there are 2 players playing, the folder looks like this:
aaaaaaaaaa

2 Likes
game.Workspace.ChildAdded:Connect(function(Player)
	if Player:FindFirstChild("Humanoid") then
		local StringValue = Instance.new("ObjectValue")
		StringValue.Value = Player
		StringValue.Name = Player.Name

		StringValue.Parent = game.ReplicatedStorage.PlayersInGame
	end
end)

This should work (I think), I havent tested it

1 Like

ok finished script is

local function SetPlayers()
	for i, Player in pairs(game.Workspace:GetChildren()) do
		if Player:FindFirstChild("Humanoid") then
			local StringValue = Instance.new("ObjectValue")
			StringValue.Value = Player
			StringValue.Name = Player.Name

			StringValue.Parent = game.ReplicatedStorage.PlayersInGame

			Player.Humanoid.Died:Connect(function()
				StringValue:Destroy()
			end)
		end
	end
end

First of all, you don’t need this.

No user’s UserId is -1 so removing it will create no difference. So, it’s better to remove it.

Also, I don’t see any problems after that line. But still, where you create the value print the name of the player.

			print(v.Name)
			local value = Instance.new("BoolValue")

well, there is like the “owner” who doesn’t need the value, that’s why is there. -1 userid is for like when testing studdio, the “Player1”

I’ll try printing their names, thanks!

Alright! The problem actually was that the remote event was firing twice, the script was in StarterGui and i changed it to ServerScriptService and it worked, I don’t know why that happened.