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

Why is the '#' in '#contestants' underlined red?

Asked by 3 years ago

I am trying to make a hide and seek game. I am trying to make it so when the seeker leaves the game or gets disconnected, then the round is over and the contestants is 0. But for some reason when I add the hashtag its underlined red. Here is the code:

-- Services
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")

local status = game.ReplicatedStorage.Values.Status

local timer = replicatedStorage.Values.Timer

local playersLeft = replicatedStorage.Values.PlayersLeft

local hitbox = serverStorage:WaitForChild("Hitbox")
--Config
local playersToStart = 2

local contestants = {}

--Functions

local function chooseSeeker(availablePlayers)
    return availablePlayers[math.random(1, #availablePlayers)]
end

local function teleportPlayers(availablePlayers,spawns) -- availablePlayers: Table / spawns: Table
    for _, plr in pairs(availablePlayers) do
        if plr.Character then
            if plr.Character:FindFirstChild("HumanoidRootPart") then
                plr.Character.HumanoidRootPart.CFrame = spawns[math.random(1, #spawns)].CFrame + Vector3.new(0,5,0)
            end
        end
    end
end

local function toMS(s)
    return ("i:i"):format(s/60%60, s%60)
end

local function addHitbox(player)

    if player then
        if player.Character then
            local character = player.Character
            local hitboxClone = hitbox:Clone()
            hitboxClone.CFrame = character.HumanoidRootPart.CFrame
            local weld = Instance.new("Weld")
            weld.Part1 = character.HumanoidRootPart
            weld.Part0 = hitboxClone
            weld.Parent = hitboxClone
            hitboxClone.Parent = character
            print("hitbox added for "..player.Name)

            return hitboxClone
        end 
    end
end

local function isContestant(plr)
    for _, contestant in pairs(contestants) do
        if contestant == plr then
            return true
        end
    end

    return false
end

local function kickOutContestant(plr)
    for i, contestant in pairs(contestants) do
        if contestant == plr then
            table.remove(contestants,i)
            print("Booted "..contestant.Name.."from the game")
        end
    end 
end

while wait(1) do

    contestants = {}

    repeat

        print("Not enough players in-game")
        status.Value = playersToStart.." players needed to start ("..#game.Players:GetPlayers().."/"..playersToStart..")"

        wait(1)

    until #game.Players:GetPlayers() >= playersToStart

    for i = 100,0,-1 do
        status.Value = "Next round starts in "..i.." seconds"
        wait(0.1)
    end

    contestants = game.Players:GetPlayers()

    local seeker = chooseSeeker(contestants)

    status.Value = "The chosen seeker is... "
    wait(1.5)
    status.Value = seeker.Name.."!"

    wait(3)

    local map = game.Workspace.TempMap

    kickOutContestant(seeker)

    teleportPlayers(contestants,map.Spawns:GetChildren())

    local hitbox = addHitbox(seeker)
    --disconnect me
    seekerHitboxConnection = hitbox.Touched:Connect(function(hit)
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if player then -- real player
            print(player.Name)
            if isContestant(player) then
                print("Real contestant")
                kickOutContestant(player)
                status.Value = player.Name.." was killed by "..seeker.Name
                if player.Character then
                    player.Character.Head:Destroy()
                    wait(2)
                end
                print("Kicked")
            end
        end
    end)

    status.Value = "Get ready to hide!"

    wait(3)

    for i = 3,0,-1 do
        status.Value = "Go Hide! "..i.." seconds remaining"
        timer.Value = toMS(i)
        playersLeft.Value = #contestants
        wait(1)
    end

    teleportPlayers({seeker},map.SeekerSpawns:GetChildren())

    status.Value = "The Seeker Has Been Released!"

    seeker.PlayerGui.SpectateGui.Enabled = false

    if seeker.Character then
        seeker.Character.Humanoid.WalkSpeed = 30
    end

    wait(3)

    local outcome = nil

    for i = 100,0,-1 do
        status.Value = "Game in Progress! "..i.." seconds until next round."
        timer.Value = toMS(i)
        playersLeft.Value = #contestants

        function playerLeaving(player)
            if player.Name == seeker.Name then
                #contestants = 0
                end
            end
        end

        game.Players.PlayerRemoving:Connect(playerLeaving)

        if #contestants == 0 then
            outcome = "SeekerWon"
            break
        end

        wait(1)
    end

    if outcome == "SeekerWon" then
        status.Value = "The seeker has won the game!"
        seeker.leaderstats.Wins.Value = seeker.leaderstats.Wins.Value + 1
        seeker.leaderstats.Coins.Value = seeker.leaderstats.Coins.Value + 70
    else
        status.Value = "Time's up everybody! The contestants have won!"
        for i, contestant in pairs(contestants) do
            contestant.leaderstats.Wins.Value = contestant.leaderstats.Wins.Value + 1
            contestant.leaderstats.Coins.Value = contestant.leaderstats.Coins.Value + 50
        end
    end

    seeker.Character.Humanoid.Health = 0
    for i, contestant in pairs(contestants) do
        contestant.Character.Humanoid.Health = 0
    end
    wait(5)

    status.Value = "End of Round"

    if seekerHitboxConnection then seekerHitboxConnection:Disconnect() print("Successfully disconnected") end

    wait(2)
end

But through lines 158-165, on line 160, the # in #contestants is underlined red. I need help please

0
# is returned as a number so suppose there were 5 contestants, #contestants = 0 is just like saying 5 = 0, that doesn't make sense, if you want to change the number of contestants then you can simply remove the "#" greatneil80 2647 — 3y

Answer this question