I am unable to make math.random work, it did tho before I made changes

local player = game.Players.LocalPlayer    
local LVL_1 = workspace.GameLevels.LVL_1:GetChildren()

function pickRandom(LVL)
	local i = math.random(1, #LVL)
    return LVL[i]
end

function tpToRandom(player, LVL)
	local Room = pickRandom(LVL)

	local Character = player.Character or player.CharacterAdded:Wait()
	local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
	HumanoidRootPart.Position = Room.TpPoint.Position + Vector3.new(0,2.5,0)
end

function start(player)
	tpToRandom(player, "LVL_1")			
end

Help

Any idea why it doesn’t work? I get this error:
[14:58:06.527 - ServerScriptService.GameScript:22: attempt to index nil with 'TpPoint']

you put LVL on the return line. Use LVL_1 as that is what you called the variable.

I know but there will also be more levels, I don’t want to return a specific one

you’re sending a string value “LVL_1”, but it should be an integer

1 Like

How would I fix that? I tried for so long

in your function start() you call the tpToRandom function. In your second parameter you put “LVL_1”, instead, put a number there without quotes.

in your pickRandom function, you only need to return i instead of LVL[i]

1 Like

You are basically a string to tpToRandom, just pass LVL_1 since it’'s a table. That would fix all of the problems.

function start(player)
	tpToRandom(player, LVL_1)			
end
1 Like

Idk if I am wrong or not but you get that error because room doesn’t exist.

HumanoidRootPart.Position = workspace.GameLevels.LVL_1["Room"..tostring(Room)].TpPoint.Position + Vector3.new(0,2.5,0)

Should fix the problem.

I can think potential problem would be the argument you parsing is nil.

I think it’s because you’re sending a string

1 Like