Is there any way to reference a variable from a different script?

I’ve got a variable for a stopwatch system for the time and I want to put the time in a “you completed the level in (time)” but I can’t find a good way of doing it without the variable thats in a different script.

1 Like

Do you mean that you have a variable representing the time in a different script but you can’t reference it from another script?

1 Like

In my GUI script, I’m trying to change text in the UI and I have a variable in the actual stopwatch script.

1 Like

in your stopwatch script, if you have a variable like this:
local time = 0

then you can’t reference that variable from a different script unfortunately

if you want to have a value that you can reference across different scripts, you’ll have to create a NumberValue instance and then manipulate it through your scripts.

so instead of local time = 0, you can create the NumberValue, then reference it in your stopwatch script and then manipulate it instead of manipulating the old value.

and you can reference the NumberValue again from your gui script and get its value

also if you don’t mind, can i see the stopwatch script?

will attributes/modulescripts work for you?

image
late response, but this is what shows up.
my stopwatch script has a format like this 00:00.00

I am also facing this issue, do you know how to fix this? Basically me and OP are probably trying to fire a reference to a variable to the server instead of firing the value.

What is the variable you’re trying to do this for and why do you need to send the reference? I don’t think this is possible, but there are definitely alternatives.

Basically I’m sending a position to the server (Mouse Position), what I am trying to do is that if the mouse position changes, it does the same on the server.

game.UserInputService.InputBegan:Connect(function(a, b)
	if a.KeyCode == Enum.KeyCode.X then
		local var = 0
		game.ReplicatedStorage.RemoteEvent:FireServer(var)
		task.wait(0.5)
		var = 2
	end
end)



-- Server:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(p, v)
	print(v) -- 0
	task.wait(1)
	print(v) -- 2
end)

This is just an example but it’s what I want it to return.

(I am aware you probably can’t do it like this, but so far I haven’t found a solution.)

1 Like

Can you use events to retrieve it whenever you actually need the mouse location?

I’m not sure if that’ll work since there’s a serverside indicator but I’ll try to do that, idk why I didn’t think of that sooner :/// Thanks

EDIT: I can’t do that, there’s a lot of things that need the pos during the windup of the attack

EDIT 2: I decided to make my own module capable of syncing variables. After it’s done I’ll post it here probably (Unless I forget or it doesn’t work properly)

1 Like

Risky, but try use _G variables.

They go over multiple scripts.

Like, for eggsample…

Script 1:


repeat
	task.wait()
until complete == true
_G[player.Name .. "Time"] = times

Script 2:

local plrtime = _G[player.Name .. "Time"]
print("You got " .. plrtime .. "!")

I don’t think this will work across client-server

use a removeevent to fire the variable, then set the _G value

1 Like

This might help. The module was very rushed, but it should still work. I’m not gonna really explain everything it does, here’s the uncopylocked game: Trash Sync Module - Roblox
Me personally I needed to constantly update mouse position, so it’s customized for that. There are examples for the usage too. Modify to your liking.