I’m trying to make some code that will fire a RemoteEvent whenever a player presses a button, so I need to fire it from a LocalScript in order to get it to work.
Because it is a LocalScript, though, it cannot get access to things only the server can access, such as ServerStorage or ServerScriptStorage. However, when I put the script (I have a script as the parent of the RemoteEvent) in some places accessible by the client as well, like ReplicatedStorage, the script doesn’t run.
Where do I put the script? I don’t really like putting scripts and other code-related stuff in the Workspace, so I’m looking for somewhere else to put it.
A LocalScript should be somewhere that’s locally copied to every client when they join (PlayerScripts, Backpack, PlayerGUI, etc.). If the button is a GUI Button, put the script inside the button. If I have a LocalScript that I don’t know where to put, I just put it inside the StarterPack (Backpack) and it works.
And the RemoteEvent should be inside the ReplicatedStorage, you can fire it there.
For a remote event to be fired from the client to the server, the local script that fires the event should be inside of the player so StarterPlayerScripts or their GUI ect. The Remote Event should be in ReplicatedStorage and the script that runs when the RemoteEvent is fired should go in ServerScriptService.
Like WoTrox said, if you put the script into ServerScriptService, it should work fine. Your server script should look something like this:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Event = ReplicatedStorage:WaitForChild("RemoteEvent")
local function Print()
print("Event fired")
end
Event.OnServerEvent:Connect(Print) --Runs the "Print" function when the remote event is fired.
You can learn more about Remote Events below. Hope this helped!
Well, I have the RemoteEvent assigned to a variable in the code, but because it’s in ServerScriptService, the LocalScript can’t get the RemoteEvent, as only the server can access stuff in ServerScriptService.
The RemoteEvent should be in ReplicatedStorage, the ServerScript should be in ServerScriptService and the LocalScript should be in PlayerScripts / StarterPack / StarterGui. It should work if they’re in the right place
Well, for some reason that isn’t the case. I have the script in the StarterPlayerScripts, but it can’t recognize the script in ServerScriptService, despite me spelling everything right.
Also, you don’t have to pass the LocalPlayer.Name when firing, because the RemoteEvent already passes the player automatically, and you can get their name there
In the original post, I said I did that, but then the script that was supposed to work didn’t run. I tested it with some print() and nothing came through, so it wasn’t running at all
As long as everything is in the right place, it should work, so something else is the problem if they don’t. Are there any errors in the output? Do you clone any of these, or are they set in the explorer?