Tonumber() help

This feels pretty simple but I guess not lol, anyways most of my code has been snipped out but heres the main part of it.

local NPCSpawnPoint
local ChosenRandomNPCSpawnPoint = math.random(1, 2)

if ChosenRandomNPCSpawnPoint == 1 then
     NPCSpawnPoint = "-28, 91, -63.5"
elseif ChosenRandomNPCSpawnPoint == 2 then
	 NPCSpawnPoint = "-28, 91, -63.5"
end

NewNPC:PivotTo(NewNPC:GetPivot() * CFrame.new(tonumber(NPCSpawnPoint)))

Error:
image

I would just make NPCSpawnPoint equal to a CFrame instead of a string. Apparently, it’s returning nil, meaning NPCSpawnPoint isn’t being assigned to any value as a variable.

but you didnt have to use tonumber

local NPCSpawnPoint

if math.random(1, 2) == 1 then
	NPCSpawnPoint = Vector3.new(-28, 91, -63.5)
elseif ChosenRandomNPCSpawnPoint == 2 then
	NPCSpawnPoint = Vector3.new(-28, 91, -63.5)
end

if NPCSpawnPoint then
	NewNPC:PivotTo(NewNPC:GetPivot() * CFrame.new(NPCSpawnPoint))
end
1 Like

You can’t turn commas into numbers. Try looking into string.split() to split the string into 3 with the separator being ", "

1 Like