Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to use a firesrever command in a script in ServerScriptService?

Asked by 3 years ago

i tried this but it is coming invalid

game.ReplicatedStorage.RemoteEvent:FireServer()

and on the other script i used

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(hit)
--Stuff here
end)
1
The client can only use ":FireServer", the server has two options ":FireClient(player)" or ":FireAllClients". WillBe_Stoped 71 — 3y
1
You can use bindable events if you need to communicate with another script. (Note: It can only do Server-Server, Client-Client.) Dovydas1118 1495 — 3y

2 answers

Log in to vote
3
Answered by 3 years ago
Edited 3 years ago

You can't.

FireServer only works in LocalScripts, which can only be ran on the local client (that's you!), while the server is where the game pretty much operates on a grand scale.

You're trying to use a RemoteEvent which is used to communicate between Server and Client. If you want to fire an event to communicate through two Scripts with the same type, use BindableEvents instead. (Works like a RemoteEvent, but only works on one side you're using)

Link to API reference of BindableEvent

Your scripts after replacing the remote to BindableEvent instead

--You don't need to put this in ReplicatedStorage as long as the script sees this
--The BindableEvent here is named BindableEvent btw
game.ReplicatedStorage.BindableEvent:Fire()

Other script:

game.ReplicatedStorage.BindableEvent.Event:Connect(function(hit)
--Stuff here
end)

Bonus: If you want it to work in a Script, here it is

--Remember to create a BindableEvent and rename it in ReplicatedStorage
local function FireServer(...)
    game.ReplicatedStorage.BindableEvent:Fire(...)
end

FireServer()

Other script

local bind = game.ReplicatedStorage.BindableEvent
bind = setmetatable({
    OnServerEvent = bind.Event
}, {__index = bind})

bind.OnServerEvent:Connect(function(hit)
--Stuff here
end)
Ad
Log in to vote
1
Answered by 3 years ago

Hello, in order to use FireServer, you will need to fire it via a LocalScript, which is used to transfer stuffs or happen things which would only happen in server-sided, for example a player would spawn a part by a TextButton, you would need a RemoteEvent for that using the FireServer for the part to be visible on server.

Answer this question