Sunx_x
(Sunx_x)
January 10, 2020, 9:56am
#1
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
Sunx_x
(Sunx_x)
January 10, 2020, 10:05am
#3
Ain’t there no first argument in onclientevent cause you can just do game.Players.LocaPlayer?
1 Like
Sunx_x
(Sunx_x)
January 10, 2020, 10:09am
#5
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()
Sunx_x
(Sunx_x)
January 10, 2020, 10:21am
#8
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)
Sunx_x
(Sunx_x)
January 10, 2020, 10:27am
#10
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
Sunx_x
(Sunx_x)
January 10, 2020, 10:29am
#12
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
Sunx_x
(Sunx_x)
January 10, 2020, 10:31am
#14
It’s not a tool, is a function in a module.
can u show the entire function?( the mma function)
Sunx_x
(Sunx_x)
January 10, 2020, 10:32am
#16
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
Sunx_x
(Sunx_x)
January 10, 2020, 10:33am
#18
Okay, let me try adding a connection into function
after u add a connection, the function needs to return it
1 Like
mishajones
(mishajones)
January 10, 2020, 11:18am
#20
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
Sunx_x
(Sunx_x)
January 10, 2020, 2:53pm
#21
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