How to make a script only work on join in roblox

Viewed 348

How do I make it so the following Roblox Lua code runs on join? I've tried wrapping it in a connect function but that makes the MouseButton1Click detector stop working.

This is the code for when you load into the game, I tried putting `Players.PlayerAdded:Connect(function(player)) with

localPlayer.CameraMode = Enum.CameraMode.Classic
MainMenuGui.Enabled = true
localPlayer.CameraMaxZoomDistance = 400
localPlayer.CameraMinZoomDistance = 0.5

around it

so it looked like:

Players.PlayerAdded:Connect(function(player)
    localPlayer.CameraMode = Enum.CameraMode.Classic
    MainMenuGui.Enabled = true
    localPlayer.CameraMaxZoomDistance = 400
    localPlayer.CameraMinZoomDistance = 0.5
end)

but the mouse button detector wouldn't work either. Sorry I'm not good at posting questions just need some help!

quitButton = script.Parent.QuitButton
playButton = script.Parent.PlayButton
mainFrame = script.Parent
MainMenuGui = mainFrame.Parent
localPlayer = game.Players.LocalPlayer
Players = game:GetService("Players")

localPlayer.CameraMode = Enum.CameraMode.Classic
MainMenuGui.Enabled = true
localPlayer.CameraMaxZoomDistance = 400
localPlayer.CameraMinZoomDistance = 0.5

playButton.MouseButton1Click:Connect(function()
    MainMenuGui.Enabled = false
    localPlayer.CameraMode = Enum.CameraMode.LockFirstPerson
    localPlayer.CameraMaxZoomDistance = 0.5
    localPlayer.CameraMinZoomDistance = 0.5
end)

My research consisted of the Roblox forum for detecting a player joining or a character joining. Also, where would I put a join detector script, and would it be a local script? I tried putting it in server scripts, character scripts, and player scripts.

2 Answers

players.playerAdded

“The PlayerAdded event fires when a player enters the game”

I’m guessing you could use this to trigger the script when someone joins, making it only work when this event is fired.

you could simply just put an if statement after your references and your made menu items, not even in a function just a simple if statement out and about, stating "if client is alive/in game then". although this might cause some issues in the long run since this is not getting called back, had this issue in a lua script for a csgo cheat.

Related