Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

"Tonumber" keeps on giving a nil value?

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

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

--Wait for players
game:WaitForChild("Players")
wait(1)
--FireClient
Remote:FireAllClients()
print("Countdown has been fired")
--FireClient
Remote.OnServerEvent:Connect(function(player)
	local Players = game.Players:GetPlayers()
	local PlayerAmount = tonumber(Players)
	print("Create Rafts")
	--Raft1
	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(Vector3.new(323.95, 0.261, -63.462))
		game.Workspace[player.Name].HumanoidRootPart.CFrame = CFrame.new(Raft1.Spawn.Position)
		wait(0.1)
		game.Workspace[player.Name].Humanoid.WalkSpeed = 0
		game.Workspace[player.Name].Humanoid.JumpPower = 0
	end
end)

1 comment

0

you cant change a array into a number

2 answers

✓ Accepted answer

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
end

while true do
	local playersActive = CheckPlayerCount()

	if playersActive > 2 then
		Remote:FireAllClients()
	end

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


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

edit: fixed the rest of your code

1 comment

0

Thanks, your credited in my game btw. Thanks for the help i'l see you when ur finished.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.