How to find a value anywhere

Sorry if this is confusing, but if I have a local script in StarterGui, and I get info from a remote function, lets say its called Name. How could I find and use name anywhere in the script, like above it and not in the function.

local smthidk
local function nameCalled(name)
    smthidk = name
end
1 Like

You can re-define a value or just pass it to another function.

Like so:

-- passed value

local function PassValue(PassedValue)
	
	print("Passed Value =", PassedValue)
	
end


-- re-defined value

local Value

local function RedefinedValue()
	
	print("Value =", Value)
	
end

-- remote event

RemoteEvent.OnCLientEvent:Connect(function(Received_Value)
	
	-- re-define value
	
	Value = Received_Value
	RedefinedValue()
	
	-- pass the value
	
	PassValue(Received_Value)
	
end)

I tried, and it shows up as nil, even if I do this:

game.ReplicatedStorage.Remotes.Rarity.OnClientEvent:Connect(function(player, rarity)
	print("Working")
	newRarity = rarity
	print(newRarity)
end)

Btw I alr defined the variable in this

1 Like

Make sure to define the variable as nil in order to avoid future errors.

local Value = nil

I put together a quick file to show the idea:

RemoteValue.rbxl (53.8 KB)

LOCAL:

local RS = game:WaitForChild("ReplicatedStorage")
local Client_To_Server_RemoteEvent = RS:WaitForChild("Client_To_Server_RemoteEvent")
local Server_To_Client_RemoteEvent = RS:WaitForChild("Server_To_Client_RemoteEvent")

task.wait(4) -- enough time for player to load for test



-- send message

Client_To_Server_RemoteEvent:FireServer("This is a test.")




-- receive message

local Old_Value = "Old test value."

print("Old Value =", Old_Value)

Server_To_Client_RemoteEvent.OnClientEvent:Connect(function(New_Value)
	
	print("CLIENT | Received:", New_Value)
	
	Old_Value = New_Value
	task.wait()
	print("Old_Value =", Old_Value)
	
end)

SERVER:

game.ReplicatedStorage.Client_To_Server_RemoteEvent.OnServerEvent:Connect(function(player, value)
	
	print("SERVER | Received:", value)
	
	game.ReplicatedStorage.Server_To_Client_RemoteEvent:FireClient(player, value)
	
end)

Screen Shot 2024-04-11 at 6.09.21 PM

1 Like

I’m doing .OnClientEvent, can you help me with that?

1 Like

I am not sure what you are asking.

OnClientEvent just means it is coming from the Server.

If you are wanting to just use Local Scripts then you don’t need a RemoteEvent.

You need a BindableEvent; it is for sending back and forth between scripts that are on the same side of the Client/Server boundry. Like Client to Client or Server to Server.

1 Like

When working with remote functions I believe you need to do this:
Remote function.OnServerEvent:Connect() = function()

end

This is so you can return a value so it’s needed

1 Like