OnServerEvent not being fired

I’m trying to make a system, so when character touches parts named “RG1, RG2, RG3, RG4… RG30” it gives player some rubies, however I made a script that Fires the RemoteEvent after touching one of those parts and made a script to connect .OnServerEvent with the :FireServer() But It’s just doesn’t work for me. It seems like OnServerEvent is not being fired, I don’t know how. I’ve try printing something when the RemoteEvent is being fired and It doesn’t work either. I’ve been also trying searching on DevForum for solutions but nothing seems to work.

Here’s my current scripts:

local part=workspace.Map.RubyGivers["RG"..math.random(1, 30)]
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and player==game.Players.LocalPlayer then
game.ReplicatedStorage["BeatLevel"]:FireServer()
end
end
end)
local player=game.Players.LocalPlayer
local event=game.ReplicatedStorage:FindFirstChild("BeatLevel")
event.OnServerEvent:Connect(function(player)
local plr=game.Players:GetPlayerFromCharacter(player)
local stats=player:FindFirstChild("HiddenStats")
local statsValue=stats:FindFirstChild("Ruby")
statsValue.Value=statsValue.Value+math.random(1, 3)
end)

Is that the script and the second one is the local script?

1 Like

yeah, the first one that i’ve uploaded there is a localscript located in game.StarterPlayer.StarterPlayerScripts
and the second one is a regular script located in game.ServerScriptService

you used local player = game.Players.LocalPlayer in a script which you cant do it

1 Like

Couple of things:
game.Players.LocalPlayer only works in LocalScripts, you can’t use it on ServerScripts

event.OnServerEvent:Connect(function(player)
local plr=game.Players:GetPlayerFromCharacter(player)
The event has already passed the Player object so there’s no need to make another variable for it, and the way you have it wouldn’t work anyway as the object passed is the player object NOT the character - so you can’t use GetPlayerFromCharacter in this instance.

1 Like

Oh my god, please indent things… anyways let me try to be a good help.

So, from what I see is your changing the value on the client-side which will only replicate it to the client and not the server. So here is a little bit of code to possible help you out:

Server:

local Remote = game.ReplicatedStorage["BeatLevel"]
Remote.OnServerEvent:Connect(function(plr)
 local Stats = plr.HiddenStats
 -- plr will provide you with the location of the player within game.Players if done right
 Stats.Ruby.Value += 1 -- Adds one ruby to the player
end)

Local: (P.S. please use touched on the server but I will use yours as a reference)

local Part = workspace.Map.RubyGivers["RG" .. math.random(1,30)]

Part.Touched:Connect(function(hit)
 local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
 if player and player==game.Players.LocalPlayer then
  game.ReplicatedStorage["BeatLevel"]:FireServer()
 end
end)
1 Like

Oh yeah, now I understand everything. I’m surprised how could I use game.Players.LocalPlayer on ServerScript :woman_facepalming:
I used your script to fix everything now. By the way I’m really sorry, I am a beginner scripter so I made common mistakes pretty often.
Anyways, Thank you guys for your help!

1 Like