Still have questions? Join our Discord server and get real time help.
Log in to vote

"Tonumber" keeps on giving a nil value? [closed]

Asked by 4 months ago
Edited 4 months ago

I'm trying to get this server script to create one raft for each player. This is what I got at the moment, any help?

The answer will be credited in my game:

local RepStorage = game:WaitForChild("ReplicatedStorage")
local Remote = RepStorage:WaitForChild("GameRemote")

--Wait for players
print("Countdown has been fired")
    local Players = game.Players:GetPlayers()
    local PlayerAmount = tonumber(Players)
    print("Create Rafts")
    for i = 1,PlayerAmount do
        local Raft1 = game.ReplicatedStorage.Minigames.StartingMinigame.CloneableRaft:Clone()
        Raft1.Parent = game.Workspace.Minigames.RaftRace.Rafts
        Raft1.Name = "Raft1"
        Raft1:MoveTo(, 0.261, -63.462))
        game.Workspace[player.Name].HumanoidRootPart.CFrame =
        game.Workspace[player.Name].Humanoid.WalkSpeed = 0
        game.Workspace[player.Name].Humanoid.JumpPower = 0
you cant change a array into a number User#23365 0 — 4mo

Locked by User#19524

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

2 answers

Log in to vote
Answered by
xPolarium 1264 Moderation Voter
4 months ago
Edited 4 months ago

To get your main question out of the way:

You're trying to using tonumber() on a table. This won't return a number since you're using it on a table. To get the length of a table you use the '#' operator before table returning the length of it. You use tonumber() to convert another data type to a number type.

local Players = game.Players:GetPlayers()
local PlayerAmount = #Players

print(PlayerAmount) --Would print the length of the 'Players' table

Now you have a lot of things in your code that you shouldn't do. One of those things is how you're defining services and using WaitForChild(). You should only use WaitForChild() when getting things like objects inside of services (I usually use it in client side of things)

When getting a service use the :GetService() method.

local RepStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

--This is an ok use of WaitForChild
local Remote = RepStorage:WaitForChild("GameRemote")

Second, you should use functions to run code that you know you'll be using later.

--Demonstration of a wait for players function:

local function CheckPlayerCount()
    local Players = game.Players:GetPlayers()
    local PlayerAmount = #Players

    return PlayerAmount

while true do
    local playersActive = CheckPlayerCount()

    if playersActive > 2 then

    wait(5) --intermission for a generic game loop

--Down here would be the rest of your OnServerEvent code.

edit: fixed the rest of your code

Thanks, your credited in my game btw. Thanks for the help i'l see you when ur finished. ZombaScripts 119 — 4mo
Log in to vote
Answered by 4 months ago

Make your local variables to global. That's all to it