#Player values being assigned but getting returned as nil for unknown reason

1 messages · Page 1 of 1 (latest)

hazy crypt
#

I'm working on my first large Roblox script that links two players into a team for an obby-style game. Each link station has two buttons. When a player steps on one, they are frozen in place (including animations), rotated to face a certain direction, and a UI appears saying “Waiting for players...” with an “Exit” button. Clicking “Exit” unfreezes them and moves them a few studs away.

When two players are on paired buttons, a 3-second countdown begins. If neither player cancels, they’re teleported to separate spawn points and placed on a shared team named after one of them.

The problem: I had a bug where one player's UI didn’t disappear after linking. To fix it, I added a remote event to disable the UI for both players once they’re linked. But once I added this, the onBothPlayersReady() function fails because the variables playerA and playerB become nil unexpectedly. The function has a safety check that exits early if either is missing, so linking never happens.

I deleted the new remote event and related code, but the issue still persists. I’ve spent hours debugging and confirmed it’s not due to canceling, death, or character respawning. Here are the two scripts involved.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local StartLinkCountdown = Remotes:WaitForChild("StartLinkCountdown")
local CancelLink = Remotes:WaitForChild("CancelLink")
local UpdateLinkStatus = Remotes:WaitForChild("UpdateLinkStatus")
local ShowWaitingStatus = Remotes:WaitForChild("ShowWaitingStatus")
local LinkConfirmed = Remotes:WaitForChild("LinkConfirmed")
local LinkFailed = Remotes:WaitForChild("LinkFailed")


local player = Players.LocalPlayer
local ui = script.Parent
local cancelButton = ui:WaitForChild("CancelButton")
local cancelPress = ui:WaitForChild("CancelPressButton")

local countDownTask = nil
local currentStation = nil
local currentSide = nil

cancelButton.MouseButton1Click:Connect(function()
    ui.Enabled = false
    if countDownTask then
        task.cancel(countDownTask)
    end
    CancelLink:FireServer(currentSide, currentStation)
end)

StartLinkCountdown.OnClientEvent:Connect(function(side, stationName)
    currentStation = stationName
    currentSide = side

    ui.Enabled = true
    cancelButton.Visible = true
    cancelPress.Visible = true

    local countdown = 3
    countDownTask = task.spawn(function()
        while countdown > 0 do
            cancelPress.Text = "Linking in " .. countdown .. "..."
            countdown -= 1
            task.wait(1)

        end
        if countdown <= 0 then
            ui.Enabled = false
            LinkConfirmed:FireServer(currentSide, currentStation)
        end
    end)
end)

UpdateLinkStatus.OnClientEvent:Connect(function(message)
    ui.Enabled = true
    cancelButton.Visible = true
    cancelPress.Visible = true
    cancelPress.Text = message
    
end)

ShowWaitingStatus.OnClientEvent:Connect(function(side, stationName)
    currentStation = stationName
    currentSide = side

    ui.Enabled = true
    cancelButton.Visible = true
    cancelPress.Visible = true
    cancelPress.Text = "Waiting for players..."
end)

LinkFailed.OnClientEvent:Connect(function()
    ui.Enabled = true
    cancelButton.Visible = true
    cancelPress.Visible = true
    cancelPress.Text = "Waiting for players..."
    
end)```

Apologies in advance for likely the poorly written code.
ivory forgeBOT
#

studio** You are now Level 2! **studio

opal gust
#

i ain't reading all that

#

from the title alone, you can't return over the wire with a remote event, you need a remote function

#

also invest time in learning how to use module scripts

hazy crypt
hazy crypt
#

@opal gust switched everything I could to remote functions and literally changed nothing

opal gust
hazy crypt
#

I did isolate the problem but it makes no sense, data.playerA and data.playerB are defined in the touched function and when they are the same linking is done all the way to the LinkConfirmed remote event (Which I changed to remote function with no difference), the end of LinkConfirmed it calls the onBothPlayersReady function and in this line of code at the very start "if not (data.playerA and data.playerB) or data.playerA == data.playerB then return end" both data.playerA and data.playerB are returned as nil for seemingly no reason and I can't find out why. I've checked everything and the problem only randomly occurred without changing anything in the server script

#

@opal gust