Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to access a Value when it is created inside a local function?

Hello! I am VERY new to LUA scripting and Roblox so please forgive my ignorance on this matter!

What I would like to accomplish is to have a StringValue added to each player as they join the game. This value will be accessed throughout the game to perform some conditional checks. I have coded the following which does add the StringValue under my player:

while true do
	game.Players.PlayerAdded:Connect(function(playerName)
		local stringVal = Instance.new('StringValue', playerName)
		stringVal.Name = "playerUnitType"
		stringVal.Value = "Civ"
	end)
	
	wait(1)
end

When I Play a test game it does indeed add a StringValue with the correct name and value as above. However, my question is how do I then access that StringValue from this script or other scripts? I tried some things such as making stringVal global instead of local but I still was not able to successfully do basic things such as print the Value or Name of the StringVal from outside of the function or the while loop.

2 answers

I hope you find my well explained answer useful, feel free to use my example if it works well.

--[[
    The issue with your code is that you're constantly creating the connection through the loop.
    Below is how I'd suggest handling it.
]]

local function OnPlayerAdded(Player) -- \\ Make it a separate function.
    local StringValue = Instance.new("StringValue")
    StringValue.Name = "playerUnitType"
    StringValue.Value = "Civ"
    StringValue.Parent = Player -- \\ It's better to parent after you set the properties, for when you decide to use WaitForChild.
end

-- \\ Now connect it to all the players, if there are any at the time of the creating this function.
for _,Player in pairs(game:GetService("Players"):GetPlayers()) do
    OnPlayerAdded(Player)
end

-- \\ Now make an RBXScriptSignal, now that we have connected it to the current players in the game.
game:GetService("Players").PlayerAdded:Connect(OnPlayerAdded)

--[[
    Here's a cool trick if this helps at all.
    When you connect a function, you can put it under a variable giving you the ability to disconnect it.

    It kinda looks like this.

    local Signal = Event:Connect(Function() 

    end)

    Signal:Disconnect() -- \\ Disconnects the "Signal" variable.

    -- \\ You can also put connections inside of a table:

    local Connections = {}
    local Connection = Event:Connect(Function)
    table.insert(Connections,Connection)

    -- \\ Then you can disconnect them all.

    for index,value in pairs(Connections) do
        if typeof(value) == "RBXScriptSignal" Then
            value:Disconnect()
        end
    end
]]

1 comment

0

This makes perfect sense. Thank you! I think what was confusing me was the PlayerAdded event. I didn't understand that event will fire automatically every time somebody joins. My logic was that we need a while loop to constantly check if somebody had joined, but now that I understand that the event fires automatically every time a player joins it makes much more sense.

You have to assign the value a parent first, otherwise it wont actually exist in the game.

game.Players.PlayerAdded:Connect(function(playerName)
    local stringVal = Instance.new('StringValue', playerName)
    stringVal.Parent = playerName -- This parents the value to the player that joined
    stringVal.Name = "playerUnitType"
    stringVal.Value = "Civ"
end)

-- And once you know where it's located, you can access it like this:

local checkValue = -- Location of where it's stored here
print(checkValue.Name) -- Test if you can access it

Also... don't put events in loops unless you only want it to fire during the loop. A while true do loop never ends, so having an event in it is pointless and can affect performance of your game.

4 comments

0

Also, do not use the 2nd parameter of Instance.new() as it is slower.

0

Set the parent of the string value to the plr.and get rid of the while loop

0

Will the game.Players.PlayerAdded event fire off automatically every time a player joins the game? I am assuming so or that wouldn't really make sense... I think in my mind for some reason I was thinking that it would just occur the first time a player joins and then not occur again.

0

@bad_santa Yes, events will fire off automatically whenever the action associated with it happens.

This discussion is preserved as it was. New answers, comments, and votes are closed.