Any way to get the variable from a function in a different part of the script?

1. What do you want to achieve? Keep it simple and clear!

i want to be able to grab the variable “player” and use it to get the PlayerGui in another part of the script that is out of the string, but when I try to put in “player” it doesn’t recognize it

image

(as expected becaues its in a different part of the script). Im unsure if i should just put the rest of the entire script into the variable, or if theres a way to get the “player” variable. my brain hurts :d

this is where i want to get the player gui so i can change the gui, but its a server script
image

  1. What is the issue? Include screenshots / videos if possible!
    image
    analysis shows that it doesnt recognize it here
1 Like

You’re looking for globals, not placing a local before a variable makes it a global which means any scope can access it.
This is a very slow thing though, so you should instead define your variable outside of the :Connect to set it inside it

local playerGui
local playerBriefings = game.Players.PlayerAdded:Connect(function(player)
	playerGui = player:WaitForChild("PlayerGui")
end)
1 Like

One thing to note is that there will be multiple players. You might want to add them to a list. As sponguswastaken said, you can use globals, either by not using local or by (better style/practice) defining the variables at the top level of your script:

-- This variable is at the top scope, so it can be used anywhere. It's basically a global but better style because you explicitly write that it exists, unlike real globals.
local mostRecentPlayerAdded = nil
Players.PlayerAdded:Connect(function(player)
    print("Got new player")
    mostRecentPlayerAdded = player
end)

-- Now you can use `mostRecentPlayerAdded` down here:
while task.wait(5) do
    if mostRecentPlayerAdded then
        print("The most recent player added is "..mostRecentPlayerAdded.Name.."!")
    end
end

If you don’t want to only get the most recent player (it’s the most recent player because it gets overridden for each new player), you can use a table:

-- Add any new players to the table
local addedPlayers = {}
Players.PlayerAdded:Connect(function(player)
    print("Got new player")
    table.insert(addedPlayers, player)
end)

-- Now you can use all the players down here:
while task.wait(5) do
    for index, player in ipairs(addedPlayers) do
        print("Player "..player.Name.." is in the game!")
    end
end

-- Make sure to also remove any players who leave from the table
Players.PlayerRemoving:Connect(function(removingPlayer)
    if table.find(addedPlayers, removingPlayer) then
        table.remove(addedPlayers, removingPlayer)
    end
end)

If you want to do the things above with PlayerGuis instead of players, that’s also possible: just switch out the players above for their PlayerGuis.

2 Likes

i changed the script to what you showed me and now i get this? i think it’s because it’s in a while true do loop?

heres the code ( i added the task.wait after i got the error to see if it would fix it but it does not)

----------(VARIABLES)----------
local winEvent = script.Parent:FindFirstChild("TeamWin")
local Current = script.Parent:FindFirstChild("Current")

local mapsFolder = game.ReplicatedStorage:FindFirstChild("Maps")
local roundMap = game.Workspace:FindFirstChild("Map")



local playerGui
local playerBriefings = game.Players.PlayerAdded:Connect(function(player)
	print("got player or something")
	 playerGui = player:WaitForChild("PlayerGui") 
end)


-- ////////////////////////////// --
task.wait(0.00001)
while true do -- Create Loop
	winEvent.OnServerEvent:Connect(function()
		if Current.Value == "Map1" then -- this string is to change to the second map
			local getMap = roundMap:WaitForChild("Map1")
			getMap.Parent = mapsFolder
			-- the string above removes the map ^
			
			----- NOTE: add victory GUI from other round system before nextUI string
			
			local nextUI = playerBriefings:FindFirstChild("2") -- FIX ERROR HERE
			nextUI.Frame.Visible = true
			-- string above makes next map UI visible
			
			
			
			local nextMap = mapsFolder:FindFirstChild("Map2")
			nextMap.Parent = roundMap
			
			
		wait(5)
		-- wait 5
		
		
		
		nextUI.Frame.Visible = false
		Current.Value  = "Map2"
		end -- this ends the map1 if statement
		-- not much more development after here
		if Current.Value == "Map2" then
			print("it is map 2, the next mao function work")
		end
	end)
	
end

Free memory leak!

No but in seriousness, you should handle that stuff on the client - anything to do with gui. It’s faster, better practice, and helps to spread server and client tasks evenly.

Creating a new connection every 1/60th of a second is also not good - this will cause the server to run out of memory very quickly. You should fix that.

1 Like

keeping that noted, how would i make it handle that in the client when the event is fired? create a local script in playerscripts or replicated first?

Well, that depends. It’s risky to use a RemoteEvent here - you don’t know how long the client will take to load the assets including that remote, but leave too long between the join and the remote, and you’ve got an evident gap that doesn’t really look great. ReplicatedFirst replicates much faster than PlayerScripts, but it’s still kinda risky.

So, I’d recommend having a RemoteFunction that the client invokes when ready. The server then returns the necessary data along that RemoteFunction that the client can then use. Just make sure to secure the remote.

1 Like

i’ve never used remote functions before, how do I use them and invoke it?

It’s basically just a function that can run on either side of the client-server boundary.

So, on the server:

RemoteFunction.OnServerInvoke = function(player: Player, someRandomParameter: unknown)
    --code
    return valuesToBeSentToClient
end

and on the client:

local valuesFromServer = RemoteFunction:InvokeServer(someRandomParameter)

RemoteFunction | Documentation - Roblox Creator Hub

alr, ill ask for more help if i dont understand anything, ty

1 Like

hey i just started coding after procrastinating and doom scrolling and I’m confused as to where to put the server string and what code you mean? i believe i am just confused but lmk

where i commented “code”? Anything the client needs from the server.

1 Like