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

Attempt to index a variable but it says its a number value?

To quote the output, " ServerScriptService.Framework:46: attempt to index local 'randomwep' (a number value)"

It's not acting up when I try to get a random map. For the record, it is a tool I am trying to find a random one of.

--Framework for Game
--With help from Hadschi

--//Variables
local rs = game:GetService("ReplicatedStorage")
local maps = rs:FindFirstChild("Maps")
local contestants = game.Players:GetPlayers()
local wep = rs.Weapons
local mapholder = workspace.MapHolder

--//Timing
local intermission = 15 --change to 30 after testing
local roundtime = 180 --maybe lengthen or shorten
local endtime = 10 --maybe 5 seconds









while true do
	mapholder:ClearAllChildren()
	wait(2)
	local allmaps = maps:GetChildren()
	local newmap = allmaps[math.random(1,#allmaps)]
	local clonemap = newmap:Clone()
	clonemap.Parent = workspace.MapHolder
	print ("Map Loaded")
	wait(2)
	local spawns = clonemap:WaitForChild("Spawns")
	for _,player in pairs(game.Players:GetPlayers()) do
		if player and player.character then
			local spawnplace = spawns.Spawn
			local torso = player.Character:WaitForChild("Torso")
			if torso then
				torso.CFrame = CFrame.new(spawnplace.Position + Vector3.new(0,3,0))
				print ("Players Spawned")
				
				local backpack = player:FindFirstChild("Backpack")
				if backpack then
					local weps = wep:GetChildren()
					local randomwep = math.random(1,#weps)
					local clonewep = randomwep:Clone()
					local randomcontestant = math.random(1,#contestants)
					clonewep.Parent = randomcontestant.Backpack
					print("Random Tool Given")
					wait(roundtime)
				end
			end
		end
	end
	--give weapons
	--last man standing
	--restart	
end

Any and all help will be appreciated!

EDIT: The answer below seemed to fix one of the problems, but when I applied the same knowledge to two lines below it, I got another error.

local randomcontestant = contestants[math.random(1,#contestants)]

It says the interval is empty, whatever that means.

3 comments

0

Don't forget to accept my answer if it helps out.

0

#weps is going to return how many weapons there are. So line 46 is essentially saying EG... "local clonewep = 3:Clone()". hopefully you can see the issue there. look at line 28 and see how it was done for the map .

0

Edited answer

2 answers

✓ Accepted answer

That is because line 45 the variable you declared is a number:

local randomwep = math.random(1,#weps)

This line on its own is not indexing the table for a random weapon. It's merely generating a pseudo-random number in the interval [1, #weps]. randomwep is actually a number. Index the weps table and this should fix the problem.

local randomwep = weps[math.random(1, #weps)] 

Hope this helps.

Edit

Your contestants variable is most likely an empty array since the script runs immediately if it's a descendant of something it can run in. The table will not update on its own. You should move your contestants variable inside of the loop wherever it is appropriate.

4 comments

0

I took too long to type the thingy. :(

0

That seems to have fixed the problem for that line, but I'm getting another error deciding a random contestant to give that weapon to. See the edit I just made, if you will.

0

Thanks! Moving it inside the loop fixed it. I would rep but im too low after I made a controversial answer to a post a long time ago :(

2

Ill rep him for you <3

Several things you have here can be improved just by stepping back and seeing the logic behind the code you've written. We can start with these constants you create when the script first runs:

local rs = game:GetService("ReplicatedStorage")
local maps = rs:FindFirstChild("Maps")
local contestants = game.Players:GetPlayers()
local wep = rs.Weapons
local mapholder = workspace.MapHolder

Everything is mostly but I want to point out your contestants variable. This obviously gets an array of the players in the server but it does it only once. What happens when a player leaves/joins? To fix this, you could move this variable into your while loop to keep an updated list of players.

The next part we could look at is your if-check for a character's Torso:

local torso = player.Character:WaitForChild("Torso")
	if torso then
		torso.CFrame = CFrame.new(spawnplace.Position + Vector3.new(0,3,0))

You want to keep in mind that Roblox has been transitioning into R15 and Anthro character rig types. These rig types both don't have a limb part named Torso. Only the R6 type has it. You would want to instead refer to the HumanoidRootPart since they all have it. The HumanoidRootPart is also set as the PrimaryPart for a player's character model. Knowing that, we could use the function :SetPrimaryPartCFrame as a better alternative in moving the player.

We could change these lines to be:

local hrp = player.Character:FindFirstChild("HumanoidRootPart") --ffc is better here
	if hrp then
		local moveTo = CFrame.new(spawnplace.Position + Vector3.new(0,3,0))
		player.Character:SetPrimaryPartCFrame(moveTo)

Addressing your main concern of the problem is how you try indexing for a weapon. Specifically these lines:

local weps = wep:GetChildren()
local randomwep = math.random(1,#weps)
local clonewep = randomwep:Clone()
local randomcontestant = math.random(1,#contestants)
clonewep.Parent = randomcontestant.Backpack

What you do here is having it choose a random number between 1 and the amount of weapons in the array with math.random. This returns you a number. In the clonewep line you try calling the :Clone() function on a number value giving your the error.

Instead you should use this number to index the weapon in your 'weps' table. In fact you do the same for the randomcontestant line. It would look like:

local weps = wep:GetChildren()
local randomwep = weps[math.random(1,#weps)]
local clonewep = randomwep:Clone()

local randomcontestant = contestants[math.random(1,#contestants)]
clonewep.Parent = randomcontestant.Backpack

If we were to bring all this together with what I've told you. The finish code would look like:

local rs = game:GetService("ReplicatedStorage")
local maps = rs:FindFirstChild("Maps")

local wep = rs.Weapons
local mapholder = workspace.MapHolder

local intermission = 15 --change to 30 after testing
local roundtime = 180 --maybe lengthen or shorten
local endtime = 10 --maybe 5 seconds

while true do
	mapholder:ClearAllChildren()
	local contestants = game.Players:GetPlayers() --updated list every new round

	wait(2)
	local allmaps = maps:GetChildren()
	local newmap = allmaps[math.random(1,#allmaps)]
	local clonemap = newmap:Clone()
	clonemap.Parent = workspace.MapHolder

	print ("Map Loaded")
	wait(2)

	local spawns = clonemap.Spawns --No use in waiting for something here
	for _,player in pairs(game.Players:GetPlayers()) do
		if player and player.Character then --player.Character
			local spawnplace = spawns.Spawn
			local hrp = player.Character:FindFirstChild("HumanoidRootPart")
			if hrp then
				local moveTo = CFrame.new(spawnplace.Position + Vector3.new(0,3,0))
				player.Character:SetPrimaryPartCFrame(moveTo)
				print ("Players Spawned")
				
				local backpack = player.Backpack--Backpack should already exist
				if backpack then
					local weps = wep:GetChildren()
					local randomwep = weps[math.random(1,#weps)]
					local clonewep = randomwep:Clone()
					
					local randomcontestant = contestants[math.random(1,#contestants)]
					clonewep.Parent = randomcontestant.Backpack
				
					print("Random Tool Given")
					wait(roundtime)
				end
			end
		end
	end
end

Let me know if you didn't understand something I said or if I missed anything.

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