Trying to get a function triggered when a player joins, but only to ever trigger ONCE

So recently I’ve been trying to do something where a script runs when a player is added, but only when the first player is added and after never running again. In less confusing terms a script that runs when a players joins ONCE and never when a 2nd or higher player joins. Could you guys help me?

Heres a screenshot:

1 Like

You could disable the script or check if it is the first player.


local players = 0

game.Players.PlayerAdded:Connect(function(plr)
players = players + 1
if players == 1 then
--run a function

else
--do nothing
end
end)

other script

game.Players.PlayerAdded:Connect(function(plr)
--run a function

script.Parent.Disabled = true
end)
1 Like

I don’t get you but i think you mean that can you use math.random in the code and well obviously yes.

If you want a script to trigger once just have it it run when the server is started. Please tell me if I’m missing something here and not understanding your question fully.

if this helps, make it run when the server starts.

Well any scripts you make will run when the server starts. So I think this answers your question?

Then you do not need to wait for a player to join event. Just run the function

1 Like

…how exactly do I do that dude?

Just call the function after you declare it.

local function foo()
  print("foo")
end

foo()
1 Like

oh. now I feel incredibly stupid.

Exactly like your script but remove the last line and just type the function name followed by ()

Don’t worry I had a near identical issue not knowing how to call a function through an event.

sometimes this doesn’t work but other times it does, im using a math.random in the script and I did everything you said, did I do something wrong? btw I used it in the way the math.random is shown in the original script

If think the issue is your if statement is generating numbers inside of it. Store your first math.random() as a variable and use the variable in your if statements.

local randomNumber = math.random(1,3)

if randomNumber == 3 then
  print("3")
end
1 Like