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

:OnServerEvent() errors, works in studio but not in-game? (FE)

Asked by
Felktor -8
6 years ago

So basically I have a script that when a remoteevent is fired it buys the object, and it works fine in studio, but I just can't seem to get it to work in-game!

error: https://gyazo.com/8fa5b2ee66feb0a44fa54a99fd96c3be

Here is the server side code (the problem code, stored in ServerScriptService):

    game.ReplicatedStorage.turretControl.OnServerEvent:connect(function(Message,Cost,planet)    
        print"good"
        planet= game.Players.LocalPlayer.PlayerGui.Main.Frame.Value.Value
    local planetID=game.Workspace.Planets:FindFirstChild(planet)
    local turret= game.ServerStorage.Turret
    local turretm= turret:Clone()
    turretm.Extra.Bought.Value=true
    turretm.Parent = planetID
    end)

Client side code (stored inside a Gui and pretty much just fires the remote event):

game.ReplicatedStorage.getMoney.OnServerEvent:connect(function(plr,Message,Cost,planet)
    local credits=plr.leaderstats.Credits.Value
    print"1"
    local turret= game.ServerStorage:FindFirstChild(Message)
    print"2"
    credits=credits-Cost
    print"3"
    print(credits)
    game.Players.LocalPlayer.leaderstats.Credits.Value=credits
    game.ReplicatedStorage.turretControl:FireServer(Message,Cost,planet)

end)

Note: I have a leader-board stored in the ScriptService also, so I don't understand the error!

2 answers

Log in to vote
0
Answered by
EB8699 30
6 years ago

Considering that's only part of the system, I'm assuming some things here.

For one, the "getMoney.OnServerEvent" I'm assuming is being fired by the server to the client. As if you've gone to the trouble of making the game FE you should know Never to trust the client with things like currency.

As such, from what I can tell all you should need to do is replace:

game.ReplicatedStorage.getMoney.OnServerEvent:connect(function(plr,Message,Cost,planet)
    local credits=plr.leaderstats.Credits.Value
    print"1"
    local turret= game.ServerStorage:FindFirstChild(Message)
    print"2"
    credits=credits-Cost
    print"3"
    print(credits)
    game.Players.LocalPlayer.leaderstats.Credits.Value=credits
    game.ReplicatedStorage.turretControl:FireServer(Message,Cost,planet)
end)

With:

game.ReplicatedStorage.getMoney.OnClientEvent:connect(function(plr,Message,Cost,planet)
    local credits=plr.leaderstats.Credits.Value
    print"1"
    local turret= game.ServerStorage:FindFirstChild(Message)
    print"2"
    credits=credits-Cost
    print"3"
    print(credits)
    game.Players.LocalPlayer.leaderstats.Credits.Value=credits
    game.ReplicatedStorage.turretControl:FireServer(Message,Cost,planet)
end)

So when the player has earned money, the Server will fire the client in a way similar to this:

game.ReplicatedStorage.getMoney:FireClient(PlayerEarned, Message, Cost, Planet)

If this doesn't answer your question, then I'd suggest showing how the getMoney event is fired.

My final thought is that while you think the error is coming from the ServerScript, it's actually coming from the client script, as in the error it states that it's coming from "Players.greenkiller2000.PlayerGui.Turret", it does not say it's coming from anything Server related.

If I'm wrong, feel free to correct me.

0
Just a side note, including where the event is originating from most definitely helps, as I didn't realize but should have that the problem was the fact that he was using a RemoteEvent to fire a event from a client to the same client, the entire purpose of the BindableEvent. Simply something to keep in mind for future readers c: EB8699 30 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You can't use "OnServerEvent" on a client! OnServerEvent must be a script in the server NOT in a client.

Also FireServer() can only be called from the client!

FireClient() can only be called from the server(server script)

OnClientEvent() must be a script in the client! (LocalScript)

I had alot of trouble with this so I ended up moving the main OnServerEvent script and the remote events to the workspace using FireServer from the client all the time.

If you're confused this is the video that explained me everything the Most simple way possible, trust me,it seems complicated but it's easy!

https://www.youtube.com/watch?v=C0qQ4lDa3t0&t=807s --You don't need to watch it all, the start explains the basics!

P.S.-In game, press F9 or Shift + F9 to check your developer console, it will tell you what error you're having and where.

0
A few things to this, first is that I'm calling OnServerEvent onto a remoteevent, second is I'm using FireServer from the PlayerGui, lastly is that I literally posted a Gyazo to the error Felktor -8 — 6y

Answer this question