|
| 25 Jul 2016 05:07 PM |
I am trying to pass a function to client from server but I have had some very strange outcomes from it. When testing in single player, it prints the name of the game and then "hello", when I want it to print the name of the player.
When I test in multiplayer(a 2 player test server) it yields an error that says func is nil, which I honestly do not understand why since I am passing the func.
Here is the script -
Server - game.Workspace.Value.Changed:connect(function(c)--[[In order to control it, I change the value manually to run the function]] game.ReplicatedStorage.RemoteEvent:FireAllClients(function() print(script.Parent.Parent.Name) --[[In single player, prints the name of the game]] end) local str = "hello" game.ReplicatedStorage.RemoteEvent:FireAllClients(function() print(str) --[[In single player, prints "hello"]] end) end)
Local - game.ReplicatedStorage.RemoteEvent.OnClientEvent:connect(function(func) func() end)
|
|
|
| Report Abuse |
|
|
| |
|
| |
|
|
| 25 Jul 2016 05:16 PM |
honestly, this is more complicated than it needs to be. i'm pretty sure whatever you're trying to do (idk the context) can be accomplished using a modulescript (while still using fe).
|
|
|
| Report Abuse |
|
|
|
| 25 Jul 2016 05:17 PM |
"Stop using FE"
why would you recommend this to anyone lol, fe doesn't create any obstacles tbh
|
|
|
| Report Abuse |
|
|
OzzyFin
|
  |
| Joined: 07 Jun 2011 |
| Total Posts: 3600 |
|
|
| 25 Jul 2016 05:22 PM |
You can't pass functions, as simple as that. This is a guess and I doubt it's even near the correct answer to why, but there's no reference to the function on the client's side hence it being nil.
print(function() end) might help you understand my guess above. |
|
|
| Report Abuse |
|
|
|
| 25 Jul 2016 05:23 PM |
I would like to do this in order to control all Player's GUIs at the same time, and do it on the client side instead of server, to minimize work on server.
|
|
|
| Report Abuse |
|
|
|
| 25 Jul 2016 05:25 PM |
@ozzy The variable func is defined as a parameter on the client side though?
|
|
|
| Report Abuse |
|
|
|
| 25 Jul 2016 05:30 PM |
In single player, I would guess that the reason it is printing stuff related to the server script because it is simplifying the script[ie finding out what script.Parent.Parent.Name is]
But I do not get why it thinks func is nil on server side. And you should be able to pass a function like this, but I'll just try and find another way to do what I am aiming for.
|
|
|
| Report Abuse |
|
|
GGGGG14
|
  |
| Joined: 29 Jan 2012 |
| Total Posts: 25344 |
|
|
| 25 Jul 2016 05:33 PM |
I don't understand why you would want to pass an entire function from the client-server or vice-versa. There are many other more efficient ways to go about this.
a) Module scripts -- this is their entire purpose; to be required and utilized in all other script types. b) RemoteEvents -- these should run separate functions in server/client through OnServerEvent & OnClientEvent (so passing a function as an argument is just silly) c) RemoteFunctions -- same thing as above & returns value d) A combination of any of the above
|
|
|
| Report Abuse |
|
|
OzzyFin
|
  |
| Joined: 07 Jun 2011 |
| Total Posts: 3600 |
|
|
| 25 Jul 2016 05:37 PM |
with reference I meant whatever the address of the function is, client does not find anything on that address and the func variable becomes nil
|
|
|
| Report Abuse |
|
|
|
| 25 Jul 2016 05:48 PM |
The main reason I wanted to do this is so I could access stuff on client side(Like a player's GUI), while using server variables(like which GUI I would want to change), and wanted to try and do this without using more then 1 remoteevent. I also wanted to change all Player's GUIs at the same time and didn't want to use threads.
I realize this is not possible now, and will probably just use separate events/functions in order to do this.
Thanks for the replies.
|
|
|
| Report Abuse |
|
|
|
| 25 Jul 2016 05:56 PM |
You wouldn't be able to do that anyways, it doesn't make sense.
|
|
|
| Report Abuse |
|
|