Where Should I Put My Script In Order For It to Run and Be Recognized by a LocalScript?

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.

Can someone help me out with this?

2 Likes

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.

1 Like

My LocalScript is in StarterPlayerScripts, the problem I’m having is where to put the Script, as I need it to be recognized by the LocalScript.

What do you mean “recognized”? To be able to run when you fire the RemoteEvent? It has to be in the SeverScriptService

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.

Is “UpMove” a script or a remote event?

He tries to :Fire it later, so its the event, which means its in the wrong place

I was writing code to move a cube whenever the player pressed a button, so I named the RemoteEvent “UpMove”

As I said before, I had the Script that the RemoteEvent was going to be effecting parented to the RemoteEvent

Move the event into ReplicatedStorage and it should work.

And of course change your variables appropriately.

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

1 Like

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

Use RemoteFunction, with that you will be able to call the server and then return the data you wanted to the local

Let me write you an example, I’ll be 2 minutes.

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?

1 Like

OH. I missed the part about putting the RemoteEvent in ReplicatedStorage, give me a bit and I’ll test it out.