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

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

Asked by 6 years ago
Edited 6 years 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: https://www.roblox.com/games/2436224110/Voteable

01local RepStorage = game:WaitForChild("ReplicatedStorage")
02local Remote = RepStorage:WaitForChild("GameRemote")
03 
04--Wait for players
05game:WaitForChild("Players")
06wait(1)
07--FireClient
08Remote:FireAllClients()
09print("Countdown has been fired")
10--FireClient
11Remote.OnServerEvent:Connect(function(player)
12    local Players = game.Players:GetPlayers()
13    local PlayerAmount = tonumber(Players)
14    print("Create Rafts")
15    --Raft1
View all 26 lines...
0
you cant change a array into a number User#23365 30 — 6y

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
5
Answered by
xPolarium 1388 Moderation Voter
6 years ago
Edited 6 years 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.

1local Players = game.Players:GetPlayers()
2local PlayerAmount = #Players
3 
4print(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.

1local RepStorage = game:GetService("ReplicatedStorage")
2local Players = game:GetService("Players")
3 
4--This is an ok use of WaitForChild
5local Remote = RepStorage:WaitForChild("GameRemote")

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

01--Demonstration of a wait for players function:
02 
03local function CheckPlayerCount()
04    local Players = game.Players:GetPlayers()
05    local PlayerAmount = #Players
06 
07    return PlayerAmount
08end
09 
10while true do
11    local playersActive = CheckPlayerCount()
12 
13    if playersActive > 2 then
14        Remote:FireAllClients()
15    end
View all 21 lines...

edit: fixed the rest of your code

0
Thanks, your credited in my game btw. Thanks for the help i'l see you when ur finished. retrobricks 162 — 6y
Ad
Log in to vote
-1
Answered by 6 years ago

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