I learn from the official ariticle about Remote fucntions and use the demo code in my game:
-- local script--
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local getUserItem = ReplicatedStorage:WaitForChild("getUserItem")
item_array = getUserItem:InvokeServer()
-- server script --
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local getUserItem = Instance.new("RemoteFunction")
getUserItem.Parent = ReplicatedStorage
getUserItem.Name = "getUserItem"
local function onGetUserItem(player)
-- do something I want....
end
getUserItem.OnServerInvoke = onGetUserItem
And my server script locates under the ReplicatedStorage
my local script locates in the " StarterPlayer-StarterPlayerScripts "
Your server scripts should be placed in ServerScriptService if you want them to run. I’m not sure if this will fix your problem, but you should do that first.
Scripts only run when they are a descendant of Workspace or ServerScriptService.
It doesn’t state anywhere on that article that the script should be placed in ReplicatedStorage. The only thing I found on that article is that RemoteFunctions should be placed inside a folder in ReplicatedStorage. Which is probably what you mean.
The aricle says " … In order for both the server and clients to utilize RemoteFunctions, the RemoteFunction object itself must be in a place where both can see it. It is recommended to store RemoteFunction in a folder inside of ReplicatedStorage…"
In the future, when you are posting code to the DevForum, please format your code in a code block. This makes it easier for us to read your code.
You can achieve a code block by typing three backticks and placing your code between.
```lua
-- Code
```
Your code right now doesn’t have any actual issues as it so stands. Remember that when you want to discover whether your code works or not, you should first debug it yourself. This can be done by using the F9 Developer Console or throwing print statements around your code and seeing what prints or doesn’t print, as well as what’s being returned from different calls.
Remember that RemoteFunctions accept function values, which are “callbacks”. This means that when you invoke a RemoteFunction, the thread that invoked the remote is expecting a return value.
-- LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local getUserItems = ReplicatedStorage:WaitForChild("getUserItems")
print(getUserItems:InvokeServer()) --> true (see below)
-- Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local function onRemoteInvoke(player)
return true -- Something has to be returned
end
local getUserItems = Instance.new("RemoteFunction")
getUserItems.Name = "getUserItems"
getUserItems.OnServerInvoke = onRemoteInvoke
getUserItems.Parent = ReplicatedStorage
If you do not return a value, then the thread that had the invocation call will permanently yield. Each of the InvokeEnvironment methods (Client/Server) expects a value to be returned; if nothing is returned, it will wait until such is done.
Anyway, aside from a little explanation on remotes, you said that the code didn’t work but you haven’t specified how it’s not working. What is the issue? Is nothing running after the remote invocation or is there an error elsewhere? You need to be specific and explain your issue in as much detail as you can so we can help you.
You should place the remote events in ReplicatedStorage,
scripts should be placed in ServerScriptService or Workspace (first one recommended).
Any script placed in Replicated- or ServerStorage will not function because those services are ment for storing objects or (ReplicatedStorage only) remotes in.
Minus ModuleScripts. Just wanted to clarify that. ModuleScripts have their sources executed on require, therefore they can run just about anywhere. They are, in essence, storages themselves.
You’re right though. Storage services, aside from ModuleScripts on require, don’t run code. The existence of ServerScriptService superseded the need to put scripts in the Workspace unless you are placing the script as a descendant of something.