.OnServerEvent isn't working?

I’m actually stumped, I’ve been trying the whole day to figure out what is wrong but I genuinely don’t know.
My script is meant to update the next station on the server, as obviously I can’t do this from the client.
I tried the script itself in the command line and it works perfectly, so it isn’t that.

Someone please help!

Client:

ReplicatedStorage.GetScheduledStop:FireServer(nextStation, route)

Server:

local routes = {
	"RO01"
}

routes["RO01"] = {
	"Leighburn",
	"Bridlea",
	"South Minstow Bridge",
	"South Minstow"
}

ReplicatedStorage.GetScheduledStop.OnServerEvent:Connect(function(currentStation, route)
	if currentStation and route then
		if routes[route] then
			local position = table.find(routes["RO01"], currentStation)

			if position and position ~= #routes["RO01"] then
				currentStation = routes["RO01"][position + 1]
			end
		end
	end
end)
5 Likes

OnServerEvent returns a player, thus currentStation returns a Player.

4 Likes

So how would you make it not return a player?

3 Likes

Exclude the Player Argument

:Connect(function(player, currentStation, route)
-- or:
:Connect(function(_, currentStation, route)
4 Likes

It’s forced to return a player. The first argument of OnServerEvent is always the player that called :FireServer. If you don’t plan on using it, you can silence it with _.

local routes = {
	"RO01"
}

routes["RO01"] = {
	"Leighburn",
	"Bridlea",
	"South Minstow Bridge",
	"South Minstow"
}

ReplicatedStorage.GetScheduledStop.OnServerEvent:Connect(function(_, currentStation, route)
	if currentStation and route then
		if routes[route] then
			local position = table.find(routes["RO01"], currentStation)

			if position and position ~= #routes["RO01"] then
				currentStation = routes["RO01"][position + 1]
			end
		end
	end
end)
3 Likes

This one and the one below still doesn’t work, I think it’s more to do with the fact that the actual event isn’t working, because even running the :FireServer in the command line doesn’t work.

4 Likes

What does route and currentStation return? What are some values that I can try plugging in?

2 Likes

currentStation could be "Bridlea"
route could be "RO01"

3 Likes

Oh, when you receive currentStation, you receive a copy of its value, not a reference to the client’s value. You’re basically changing the copied value. You may want to look at RemoteFunctions instead.

Here’s a re-write using a RemoteFunction:

-- Server
local routes = {
	"RO01"
}

routes["RO01"] = {
	"Leighburn",
	"Bridlea",
	"South Minstow Bridge",
	"South Minstow"
}

-- RemoteFunctions have a property "OnServerInvoke". It is NOT an event.
ReplicatedStorage.GetScheduledStop.OnServerInvoke = function(player, currentStation, route)
	if not (currentStation and route) then
		return nil
	end
	
	if not routes[route] then
		return nil
	end
	
	local position = table.find(routes[route], currentStation)

	if position and position ~= #routes[route] then
		return routes[route][position + 1]
	end
	
	return nil
end
-- Client code to test:
local repStore = game:GetService("ReplicatedStorage")

local nextStop = repStore.GetScheduledStop:InvokeServer("Bridlea", "RO01")

if not nextStop then print("Nowhere to go")
else print(`Going to {nextStop}`) -- "Going to South Minstow Bridge"
end

RemoteFunctions require a return value (even nil) at all times.

Just don’t use OnClientInvoke or InvokeClient() from the server, as invoking the client can possibly be used against your game.

2 Likes
ReplicatedStorage.GetScheduledStop.OnServerEvent

always has player as the first param

so you need to accomodate that

function(player, currentStation, route)
3 Likes

Thanks, already tried that, but didn’t work.

1 Like

The script works, but when I put it into my own code, it yields for seemingly forever and it doesn’t show anything in output.

2 Likes

Again, I simply think that for some reason the remotes aren’t running…

2 Likes

Can you share with us the new code? I’m curious how you used the code I sent.

1 Like

Okay,

Client

if isInPlatform == true and Velocity == 0 and TRTS == false then
	isLoading = true
	eBrakeActive = true

	task.wait(20)

	eBrakeActive = false
	isLoading = false
	TRTS = true
elseif TRTS == true then
	doorsClosing = true

	task.wait(4)
			
	local newNextStop = ReplicatedStorage.GetScheduledStop:InvokeServer("Bridlea", "RO01")

	if newNextStop then
		stationIndicator.NextStation.Text = newNextStop
	end	
			
	doorsClosing = false
	TRTS = false
	hasLoaded = true
end

Server:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local routes = {
	"RO01"
}

routes["RO01"] = {
	"Leighburn Junction",
	"Bridlea",
	"South Minstow Bridge",
	"South Minstow"
}

ReplicatedStorage.GetScheduledStop.OnServerInvoke = function(player, currentStation, route)	
	if not (currentStation and route) then
		return nil
	end

	if not routes[route] then
		return nil
	end

	local position = table.find(routes[route], currentStation)

	if position and position ~= #routes[route] then
		return routes[route][position + 1]
	end

	return nil
end

return routes
2 Likes

I can’t seem to replicate the issue.

Judging by the last line in the server script, is it a ModuleScript? If so, is it not yet require()d by any other Scripts?

1 Like

It is a module script, and no it isn’t yet required.

1 Like

ModuleScripts don’t run on their own. Either convert it to an ordinary Script or have another Script (perhaps placed underneath it) and require() it.

1 Like

Well, it needs to be a module script, so I’ll opt for the other option.

1 Like

Thank you so so much! It worked! Can’t believe that was the problem.

2 Likes