Function running before being called?

Hello all! I have this script that is just really making me mad as its not working, But currently I think the problem is because players.playerremoving and humanoid.died are both firing before I even call them, which then causes an error and pauses the whole function. This is only my theory as I’m not able to test through the server and multiple players for some reason. If someone else can spot any other errors that would be greatly appreciated

local minPlayerCount = 2
local repStorage = game.ReplicatedStorage
local inGame = repStorage.inGame
local intermissionTime = 10
local intervalTime = 100
local spinner = game.Workspace.spinner
local Players = game:GetService("Players")
local currentPlayers = {}

local removeplayers = function (player)
	print(player.Name.." died")
	table.remove(currentPlayers, table.find(currentPlayers, player))
		if #currentPlayers:GetChildren() <= 1 then
		inGame.Value = false
		player.Character.HumanoidRootPart.CFrame = game.Workspace.SpawnLocation.CFrame + Vector3.new(0,5,0)
	end
end

Players.PlayerRemoving:Connect(removeplayers())

while true do
	wait(5)
	local multi = 1
	if #Players:GetChildren() >= minPlayerCount then
		inGame.Value = true
		for i,v in pairs(Players:GetChildren()) do
			table.insert(currentPlayers,i,v)
			v.Character.HumanoidRootPart.CFrame = game.Workspace.startPos.CFrame
			v.Character.Humanoid.Died:Connect(removeplayers())
			end
		if inGame.Value == true then
			for i = intermissionTime, 1, -1 do
				repStorage.gameStatus.Value = "Time remaining in intermission: "..i
				wait(1)
			end
			while inGame.Value == true do
				for i = intervalTime, 1, -1 do
					spinner.CFrame = spinner.CFrame*CFrame.fromEulerAnglesXYZ(0,(0.1*multi),0)
					wait(.01)
				end
				local multi = multi*5
				end
			end
		end
	end



Functions are stored like variables, but they’re called with “()”.

In these events you’re calling the function instead of passing it as an argument so it can be internally called when a player lefts the server or dies. Instead, pass it as a variable.

Players.PlayerRemoving:Connect(removeplayers)
v.Character.Humanoid.Died:Connect(removeplayers)

2 Likes

Use a variable instead. And remove the ‘()’ after removeplayers. @Sarchyx has explained it above me.

1 Like

How do I do this if I want to pass arguments within the brackets I had initially?

did you find a fix for adding a variable?