Attempt to call table value when randomizing starting position

1. What do you want to achieve? Keep it simple and clear!
I want to make system to randomize the starting position of my tween.
2. What is the issue? Include screenshots / videos if possible!
It throws me an error

local Liftiem = game.workspace.Liftiem
local Finish = game.Workspace.Finish
local start = game.workspace.Start
local liftiemtable = {}

while true do
	
	
	local startpos1 = 0
	function randomize()
		 local startpos = math.random(1,3)
		if startpos == 1 then 
			startpos1 = CFrame.new(start.CFrame * CFrame.new(Vector3(0,0,11)))
		elseif startpos == 2 then
			startpos1 = CFrame.new(start.CFrame)
		else
			startpos1 = CFrame.new(start.CFrame * CFrame.new(Vector3(0,0,-11)))
			spawnliftiem()
		end
	end
	
function spawnliftiem()
	local liftiems = Liftiem:Clone()
	local liftiemss = liftiems.PrimaryPart
	liftiemss.CFrame = startpos1
	liftiems.Parent = game.Workspace
		table.insert (liftiemtable,liftiems)
		print(liftiemtable)
	
	
end


	for i, v in pairs (liftiemtable) do
local liftiemroot = v:FindFirstChild("HumanRootPart")
local Info = TweenInfo.new(5,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,1)
local LiftiemTween = TweenService:Create(liftiemroot,Info,{CFrame = Finish.CFrame})
		LiftiemTween:Play() 
local humanoid = v:FindFirstChild("Humanoid")
local loadanimation = humanoid.Animator:LoadAnimation(v.Animation)
		loadanimation:Play()
		LiftiemTween.Completed:Wait()
		liftiemtable[1]:Destroy()
		table.remove(liftiemtable,1)
		print(liftiemtable)
	end
	


    wait(6)
	randomize()
end

The most important clue in the error message is the number 20 after the name of your script, which is the line number where the error occurred. There’s no way for us to tell which line that is though, because you might have omitted some lines of code from your own script and whatnot. So please tell us which line that is.

1 Like

startpos1 = CFrame.new(start.CFrame * CFrame.new(Vector3(0,0,-11)))
This line

should be Vector3.new

there are also another line which you missed

startpos1 = CFrame.new(start.CFrame * CFrame.new(Vector3(0,0,11)))
-- should be
startpos1 = CFrame.new(start.CFrame * CFrame.new(Vector3.new(0,0,11)))

Hmm it throws me a new error

startpos1 = CFrame.new(start.CFrame)
here’s line 18

change that into

start.CFrame

Edit: actually remove the CFrame.new in the marked areas here

		if startpos == 1 then 
			startpos1 = ~CFrame.new~ (start.CFrame * CFrame.new(Vector3(0,0,11)))
		elseif startpos == 2 then
			startpos1 = ~CFrame.new~(start.CFrame)
		else
			startpos1 = ~CFrame.new~(start.CFrame * CFrame.new(Vector3(0,0,-11)))
			spawnliftiem()
		end
1 Like

@Lielmaster is correct - you’re incorrectly settting the position part of the CFrame as a CFrame, whereas they should be:

	function randomize()
		 local startpos = math.random(1,3)
		if startpos == 1 then 
			startpos1 = start.CFrame * CFrame.new(Vector3(0,0,11))
		elseif startpos == 2 then
			startpos1 = start.CFrame
		else
			startpos1 = start.CFrame * CFrame.new(Vector3(0,0,-11))
			spawnliftiem()
		end
	end

Also, in your randomize function - only your your ‘else’ statement will call the spawnliftiem() function - I think you meant to move it outside of the if logic, so all 3 scenarios can call it correct?

	function randomize()
		 local startpos = math.random(1,3)
		if startpos == 1 then 
			startpos1 = start.CFrame * CFrame.new(Vector3(0,0,11))
		elseif startpos == 2 then
			startpos1 = start.CFrame
		else
			startpos1 = start.CFrame * CFrame.new(Vector3(0,0,-11))
		end
		spawnliftiem() -- move here?
	end

By the way, did you know you can do the same thing above with just one line of code?

startpos1 = start.CFRame * CFrame.new(0, 0, math.random(-1,1) * 11)
-- if the random is -1 == -11, 0 == 0, 1 = 11 :)
1 Like

Oh I just realized, my bad. Anyways, it works, thanks all.

don’t forget to mark @Lielmaster response as the solution so this topic can be closed. :slight_smile: