How do i make this script work

hello im new to scripting and can someone help me make this work they said i should make it remote event but idk how to turn it to remote event

this is in a server script service it detects when a new player joins a different script enables, thx

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local playerDataStore = dataStoreService:GetDataStore("PlayerData")


players.PlayerAdded:Connect(function(player)
	local success, data = pcall(function()
		return playerDataStore:GetAsync("Player_"..player.UserId)
	end)

	if success and data then
		print(player.Name.." old")
		game.StarterGui.MainMenu.LocalScriptold.Enabled = true
	else
		print(player.Name.." new")
		game.StarterGui.MainMenu.LocalScript.Enabled = true
		local success, err = pcall(function()
			playerDataStore:SetAsync("Player_"..player.UserId, true)
		end)
		-- failed
		if not success then
			warn("error savnig data "..player.Name..": "..err)
		end

	end
end)

I’d suggest something like this:

local players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")

players.PlayerAdded:Connect(function(player)
	local PlayerGui, Menu = player.PlayerGui, player.PlayerGui:WaitForChild("MainMenu")
	local Success, Data = pcall(function()
		return DataStore:GetAsync("Player_" .. player.UserId) or false
	end)
	
	if Success and Data then
		print(player.Name .. " old")
		Menu.LocalScriptold.Enabled = true
	else
		print(player.Name .. " new")
		Menu.LocalScript.Enabled = true
		local success, Error = pcall(function()
			DataStore:SetAsync("Player_" .. player.UserId, true)
		end)
		
		-- failed
		if not success then
			warn("Error saving data " .. player.Name .. ": " .. Error)
		end
	end
end)

Just a couple of modifications to make it a bit cleaner, also added a WaitForChild incase of timing issues, also in that script you’re using StarterGui which won’t effect the player if all their UI is already loaded in.

When a script is an instance below the datamodel (game) and isn’t enabled, enabling it won’t run the script as it was originally disabled. You have to use remote events for your request.

Feel free to read the documentation linked or folllow these steps:

  1. Create a remote event (preferably on replicatedstorage).
  2. Fire it from the server whenever needed (ex: game.ReplicatedStorage.RemoteEvent:FireClient(player))
  3. Next, you have to receive it from the client, this can be done like this:
    game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
         -- whatever you want to do here
    end)
    

Replace that code with this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerStatus = ReplicatedStorage:WaitForChild("PlayerStatus")

players.PlayerAdded:Connect(function(player)
	local success, data = pcall(function()
		return playerDataStore:GetAsync("Player_"..player.UserId)
	end)

	if success and data then
		-- player is old
		PlayerStatus:FireAllClients(false, player.Name)
	else
		-- player is new
		PlayerStatus:FireAllClients(true, player.Name)
	end
end)

But then add a localscript with this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerStatus = ReplicatedStorage:WaitForChild("PlayerStatus")

PlayerStatus.OnClientEvent:Connect(function(isNew, playerName)
	if isNew then
		-- player is new
		game.StarterGui.MainMenu.LocalScript.Enabled = true
	else
		-- player is old
		game.StarterGui.MainMenu.LocalScriptold.Enabled = true
	end
end)

Basically this localscript listens for the RemoteEvent and enables the script based on the result.

Didn’t test this; basically just added more error handling and switched to PlayerGui to ensured proper script paths, and kept the logic clean.

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local playerDataStore = dataStoreService:GetDataStore("PlayerData")

players.PlayerAdded:Connect(function(player)
    local success, data = pcall(function()
        return playerDataStore:GetAsync("Player_"..player.UserId)
    end)

    if not success then
        warn("Error retrieving data for "..player.Name..": "..data)
        return
    end

    if data then
        print(player.Name.." is an old player")
        local playerGui = player:WaitForChild("PlayerGui")
        local mainMenu = playerGui:WaitForChild("MainMenu")
        local localScriptOld = mainMenu:WaitForChild("LocalScriptold")
        localScriptOld.Enabled = true
    else
        print(player.Name.." is a new player")
        local playerGui = player:WaitForChild("PlayerGui")
        local mainMenu = playerGui:WaitForChild("MainMenu")
        local localScript = mainMenu:WaitForChild("LocalScript")
        localScript.Enabled = true

        local success, err = pcall(function()
            playerDataStore:SetAsync("Player_"..player.UserId, true)
        end)

        if not success then
            warn("Error saving data for "..player.Name..": "..err)
        end
    end
end)