#How do i use remote events in Roblox Studio?

1 messages Β· Page 1 of 1 (latest)

raw rune
#

So i have a screen gui that says "Wheat: 0" and then whenever a remote event is fired it's supposed to increase it by 1 everytime. The issue is I can't get the remote event to fire, and I don't know if i used it in the right context

The wheat is in workspace.wheat

The remote event is in Replicated Storage

The gui is in StarterGui

this code is from the wheat object:

`local wheat = script.Parent

local position = wheat.Position
local rotation = wheat.Orientation

local rep = game:GetService("ReplicatedStorage")
local fireEvent = rep.CollectedWheat

wheat.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
fireEvent:FireServer()
end
end)`

disclaimer: I removed a lot of code for better context because everything else is working perfectly aside from the FireServer()

#

oops that didn't copy paste properly

hollow ice
#

looks like you're using :FireServer instead of :FireClient(player)

raw rune
hollow ice
#

:FireServer() goes from client to server. this means that the client can tell the server about something.
:FireAllClients() goes from the server to ALL players.
:FireClient(player) goes from the server to ONLY player.

#

you might want to keep the player reference if you want to fire the event to the player

#
wheat.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.parent)
    if player then
        fireEvent:FireClient(player)
    end
end)
#

in lua, nil is considered false when you use it inside an if statement.

raw rune
#

`local label = script.Parent

local rep = game:GetService("ReplicatedStorage")
local remoteEvent = rep.CollectedWheat

local wheat = 0

remoteEvent.OnServerEvent:Connect(function(player)
print("server fired successfully")
end)`

#

this is the script inside my gui label

#

it doesn't print, i assume i'm missing parameters

hollow ice
#

is this on the server or the client?

raw rune
#

i'm just checking if it receives the server fire, but idk how to work with remote events sorry

#

this is client i think, under StarterGui-ScreenGui-TextLabel-Script

#

that's the hierarchy

hollow ice
#

should it be OnClientEvent?

raw rune
#

Oh let me try that, thanks

#

"OnClientEvent can only be used on the client"

#

do i move the gui elsewhere?

hollow ice
#

is that an error?

raw rune
#

yes

hollow ice
#

are you sure this script is on the client?

#

and not the server

raw rune
#

i'm not sure. if i make it a localscript will it be client-side?

hollow ice
#

yes

raw rune
#

oh

hollow ice
#

localscripts are client side only

#

scripts are server side

raw rune
#

okay thank you i'll go fix that

#

it printed this time, thank you so much

hollow ice
#

np

raw rune
#

i'm new to multiplayer coding so i'm still learning client-server interactions