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.
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)
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)
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.
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.
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