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

Looking to program/fix my "Ready Up" button?

Asked by 3 years ago
Edited 3 years ago

Hey, I'm trying to build a sort of 'Ready Up' button. Essentially, when enough players in the game click the button (8 in this situation), the game begins, although I haven't programmed that part yet.

Right now, I'm working on listing how many players have actually pressed the button in the "TL" variable as seen in my script below:

local n = 0 
local RemoteEvent = game.ReplicatedStorage.RemoteEvent
local RemoteEvent2 = game.ReplicatedStorage.RemoteEvent2
local debounce = false
local debounce2 = false

RemoteEvent.OnServerEvent:Connect(function(plr)
    local TL = plr.PlayerGui.MainMenu.PlayMenu.TextLabel
    local ReadyUpButton = plr.PlayerGui.MainMenu.PlayMenu.Frame.Ready
    ReadyUpButton.MouseButton1Click:Connect(function()
        if debounce == false then
            debounce2 = false
            n = n + 1
            TL.Text = n .. "/8 Players"
            debounce = true
        end
    end)
end)

RemoteEvent2.OnServerEvent:Connect(function(plr)
    local TL = plr.PlayerGui.MainMenu.PlayMenu.TextLabel
    local BackButton = plr.PlayerGui.MainMenu.PlayMenu.Frame.Back
    BackButton.MouseButton1Click:Connect(function()
        if debounce2 == false then
            debounce2 = true
            debounce = false
            wait(2)
            n = n - 1
            TL.Text = n .. "/8 Players"
        end
    end)
end)

For context, there are 2 screens in my game. When you start the game, there is a "Main Menu" screen which consists of a "Play Button" that takes you to another screen. This screen has both the 'BackButton' and 'ReadyUpButton'. The BackButton takes you back to the Main Menu and the Ready Up Button readies you up (or increases n in this case). 'n' is the amount of players that have ready'd up, so when you press the back button n decreases by 1 and when you press ready up n increases by 1.

Main Menu

Play Menu

I am pretty new to Remote Events (and scripting in general at that), so there are a few bugs with the "Readying Up" part.

Firstly, when actually testing this script on a public server, if one player presses the "ReadyUpButton", 'n' doesn't go up by one on other player's screens but it does with the local player. For the number to actually go up on other player's screens, they have to press the 'BackButton' and then the 'ReadyUpButton' again. This isn't the only problem. If one player has already Ready'd up, the 'ReadyUpButton' does not increase 'n'.

Can I get some help here?

1 answer

Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
3 years ago
Edited 3 years ago

This is not the proper way to use Remote Events. Click Event connection should be done in the local script, and only after a click a Remote Event should be fired. There are many issues, but the worst is that you make a new connection everytime an event is fired.

You would need to provide your local scripts to get an accurate answer, but I will try my best to do an educated guess:

First create new event in replicated storage called UpdateCountEvent

Next your local scripts. Make a new one, if you do not have it, and parent it to the gui button: Ready button (debounce is not needed in click events):

local button = script.Parent
local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")

button.MouseButton1Click:Connect(function()
    RemoteEvent:FireServer()    
end)

BackButton:

local button = script.Parent
local RemoteEvent2 = game.ReplicatedStorage:WaitForChild("RemoteEvent2")

button.MouseButton1Click:Connect(function()
    RemoteEvent2:FireServer()
end)

Following local script should be parented to the text label

local TL = script.Parent
local UpdateCountEvent = game.ReplicatedStorage:WaitForChild("UpdateCountEvent")

UpdateCountEvent.OnClientEvent:Connect(function(n)
    TL.Text = n .. "/8 Players"
)

Now the server script

local RemoteEvent = game.ReplicatedStorage.RemoteEvent
local RemoteEvent2 = game.ReplicatedStorage.RemoteEvent2
local UpdateCountEvent = game.ReplicatedStorage.UpdateCountEvent

local n = 0
local participatingPlayers = {}
RemoteEvent.OnServerEvent:Connect(function(player)

    if participatingPlayers[player] then
        return
    end
    n = n + 1
    participatingPlayers[player] = true
    UpdateCountEvent:FireAllClients(n)

end)
RemoteEvent2.OnServerEvent:Connect(function(player)

    if not participatingPlayers[player] then
        return
    end
    n = n - 1
    participatingPlayers[player] = nil
    UpdateCountEvent:FireAllClients(n)

end)

--Edit (see below)
local CheckPlayerCount = game.ReplicatedStorage.CheckPlayerCount

local function OnCheckPlayerCount()
    return n
end
CheckPlayerCount.OnServerInvoke = OnCheckPlayerCount

--Edit2:
game.Players.PlayerRemoving:Connect(function(player)
    if participatingPlayers[player] then
        participatingPlayers[player] = nil
        n = n -1
        UpdateCountEvent:FireAllClients(n)
    end
end)

I gtg now, but I will be back later in case of problems.

Edit: You'll still need to let joining players know how many participants are Ready Up. Lazy way would be to fire UpdateCountEvent inside a function connected to PlayerAdded Event. However you may risk, that the gui has not loaded yet. Some people add wait, before firing, but I prefer more elegant solution: Add a RemoteFunction in ReplicatedStorage and name it CheckPlayerCount Add to your server script this lines (I also edited answer above):

local CheckPlayerCount = game.ReplicatedStorage.CheckPlayerCount

local function OnCheckPlayerCount()
    return n
end
CheckPlayerCount.OnServerInvoke = OnCheckPlayerCount

Finally modify update script like this:

local TL = script.Parent
local UpdateCountEvent = game.ReplicatedStorage:WaitForChild("UpdateCountEvent")
local CheckPlayerCount = game.ReplicatedStorage:WaitForChild("CheckPlayerCount")

TL.Text = CheckPlayerCount:InvokeServer() .. "/8 Players"

UpdateCountEvent.OnClientEvent:Connect(function(n)
    TL.Text = n .. "/8 Players"
)

Edit 2: To complete your script, it is a good idea to remove players that have quit the game, being impolite and not pressing back button before leave: Add few more lines to your local script (I edited the answer above too):

game.Players.PlayerRemoving:Connect(function(player)
    if participatingPlayers[player] then
        participatingPlayers[player] = nil
        n = n -1
        UpdateCountEvent:FireAllClients(n)
    end
end)
0
This was EXTREMELY useful. I have yet to understand a couple things that you mentioned here and the specific details of how this works, but I will be sure to look into it. Thank you! the_XBOSS 2 — 3y
Ad

Answer this question