Search code examples
luaroblox

Bailing people out of prison in roblox using Remote Events doesn't seem to work properly


Here's the deal. When you get arrested in my game, you get sent to jail. To get out you must be bailed out.

The client sends a request to the server to bail them. Every other part seems to work except this part I believe, however it could be the client side script. Is there anything incorrect about this script? I have checked it for any errors that are obvious to me.

local replicatedStorage = game:GetService('ReplicatedStorage')
local createSystemMessage = replicatedStorage:WaitForChild('CreateSystemMessage')

game.ReplicatedStorage.Bail.OnServerEvent:Connect(function(Player,PlayerToBail)
        Player = game.Players:FindFirstChild(Player)

        local tab = nil
    for i,v in pairs(_G.GlobalData) do
        if v.Name == Player.Name then
            tab = v
        end
    end
    if PlayerToBail.Team == game.Teams:FindFirstChild("Criminal") then
        local Bounty = PlayerToBail.leaderstats.Bounty.Value * 2
     if tab.Bank <= Bounty then
        tab.Bank -= Bounty
            PlayerToBail.leaderstats.Bounty.Value = 0
            PlayerToBail.Prisoner.Value = false
            PlayerToBail.Team = game.Teams:FindFirstChild("Civilian")
            createSystemMessage:FireAllClients((Player.Name .. ' has Bailed ' .. PlayerToBail.Name), Color3.fromRGB(0, 250, 0))



end
end
    
end)

And the local script which works:

script.Parent.AcceptButton.MouseButton1Click:Connect(function()
            local PlayerName = script.Parent.TargetName.Text
            game.ReplicatedStorage.Bail:FireServer(PlayerName)
            print ("Bail Requested")
end)

Solution

  • I believe the problem occurs with the argument you're passing to the remote event. In your client script you pass PlayerName as an argument. I'm assuming this is a string of the player's name:

    game.ReplicatedStorage.Bail:FireServer(PlayerName)
    

    PlayerName will actually be sent to the parameter "PlayerToBail", which I'm assuming is supposed to be a player object. Keep in mind that Roblox's RemoteEvents automatically pass in the player who fired the remote event as the first argument. So the "Player" parameter of the function connected to your remote event is the actual player object that has the local script that fired the remote event.

    Instead, I would fire the remote event like this:

    game.ReplicatedStorage.Bail:FireServer()
    

    Since the player you want to bail is automatically passed as an argument, you don't need to add any additional arguments to FireServer. In addition, you would get rid of the "PlayerToBail" parameter in your server script.

    Also note, it is unnecessary to have this line of code in your server script:

    Player = game.Players:FindFirstChild(Player)
    

    Player is already referring to an object in game.Players. In addition, Player is an object, not a string. So this would not work. You can simply use Player as is for your purposes.

    More info on Remote Events: https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

    Please feel free to follow up if you still have issues.