OnClientEvent doesn't set number

Hi!
I’m trying to make event that sets a number.

So my problem is OnClientEvent sets the value only inside the function.
But when I print test outside it prints nil

Script:

local remote = [path]
local test

remote.OnClientEvent:Connect(val)
   test = val
   print(test)
end)
print(test)

In output is like this:

remote.OnClientEvent:Connect(val)
   test = val
   print(test) --prints 5
end)
print(test) -- prints nil

I tried making a table, and insert val in it, but still is nil

first at


remote.OnClientEvent:Connect(val)


the first argument is player, ur code needed to be


local remote = [path]
local test

remote.OnClientEvent:Connect(plr,val)
   test = val
   print(test)
end)
print(test)

and u put print(test) after the Event, that results it will print before the event is changed and test will be 5

Ain’t there no first argument in onclientevent cause you can just do game.Players.LocaPlayer?

1 Like


There no player argument in OnClientEnvet

oh,my mistake, at FireClient() the first parameter is the player, ur problem in the script is the OnClientEvent is called later than the print()

There a existing code:

local plr = game.Players.LocalPlayer
local load= game.ReplicatedStorage.Load
local mma

function load.OnClientInvoke(cmd)
	if cmd == "Load" then
		mma = LOAD_MMA()
	elseif cmd == "Unload" then
		mma:Disconnect() --gives a error, (mma is nil)
		mma = nil
	end
end

(sorry im really bad at explaining xd)

i have some questions, why u use

local load= game.ReplicatedStorage.Load

and what it does? cannot u only use



local load = {}

?

and can u show the LOAD_MMA() part because it should be like this(this is a short example)



local function LOAD.MMA()
       local con ; con = workpace.Part.Touched:Connect(function()

       end)
       return con
end)

Load mma is combat script, without any loading.
conn is for loading it.

if the mma don’t return any Connection, it can’t be disconnected

Im using connections in local script, so i can unload them when player dead or something

if it is a tool and player dies, the tool diapears from his hand and the Connection disconnect automatic and can u give me a model or sript with the mma

It’s not a tool, is a function in a module.

can u show the entire function?( the mma function)

What the sence, just a function that doesn’t return anything

if it don’t return anything then that make all to don’t work, it needs to return a Connection if u want to can Disconnect it

Okay, let me try adding a connection into function

after u add a connection, the function needs to return it

1 Like

It seems you are confused about how scoping works. I think you will solve this by yourself by just reading this article (at least the ‘Using the “local” statement’ section).

2 Likes

I finally found a fix, thanks to @mishajones

While I was loading the module, everything I defined was outside the module, so that’s why it wasn’t loading

1 Like