List of object problems

Im making a worm for my game that has a couple behaviors it will cycle though. I put these behaviorObjects in two list: p1behaviors and p2behaviors . I would like to make it so that I can change the variable currentBehavior back and forth between those two list. When I try do to do so I get an error. That error being: Workspace.ClientSidedObjects.MajorCCOs.SunWorm.ClientObjectScript:342: attempt to index nil with ‘behaviorName’

The line it is erroring at bellow is the one:
local newBehavior = behavior:cloneBehavior(currentBehavior[behaviorsCycled % 8 + 1])
And both of these do work:
local newBehavior = behavior:cloneBehavior(p1Behavior[behaviorsCycled % 8 + 1])
local newBehavior = behavior:cloneBehavior(p2Behavior[behaviorsCycled % 8 + 1])

This is the entire behavior system:
||
–Difines worm’s behavior class
local behavior = {}
function behavior.new(behaviorName, initialSpeed, initialRotSpeed)
local newBehavior = {}

	newBehavior.behaviorName = behaviorName
	newBehavior.speed = initialSpeed
	newBehavior.rotSpeed = initialRotSpeed

	return newBehavior
end

--Initializes behavior objects to be returned
function behavior:getBehavior(behaviorName)
	if behaviorName == "hook" then
		return behavior.new("hook", 20, 5)
	elseif behaviorName == "charge" then
		return behavior.new("charge", 20, 10)
	elseif behaviorName == "loop" then
		return behavior.new("loop", 30, 3)
	elseif behaviorName == "turtle" then
		return behavior.new("turtle", 20, 0.5)
	else
		error("Invalid behaviorName")
	end
end


local p1Behavior = {behavior:getBehavior("hook"), 
	behavior:getBehavior("charge"), 
	behavior:getBehavior("loop"), 
	behavior:getBehavior("hook"), 
	behavior:getBehavior("loop"), 
	behavior:getBehavior("hook"), 
	behavior:getBehavior("charge"), 
	behavior:getBehavior("turtle")
}
local p2Behavior = {behavior:getBehavior("turtle"), 
	behavior:getBehavior("charge"), 
	behavior:getBehavior("charge"), 
	behavior:getBehavior("loop"), 
	behavior:getBehavior("turtle"), 
	behavior:getBehavior("hook"), 
	behavior:getBehavior("charge"), 
	behavior:getBehavior("hook")
}

local behaviorsCycled = 0
local currentBehavior = p1Behavior
--Returns a clones behavior with the behaviorNames default values
function behavior:cloneBehavior(behaviorToClone)
	local tempBehavior = behavior.new(behaviorToClone.behaviorName, behaviorToClone.speed, behaviorToClone.rotSpeed)
	return tempBehavior
end

local function getNewBehavior()
	local newBehavior = behavior:cloneBehavior(currentBehavior[behaviorsCycled % 8 + 1])
	behaviorsCycled += 1
	currentBehavior = newBehavior
	print(currentBehavior.behaviorName)
end
getNewBehavior() --Sets current behavior

||

Edit: They are NOT indentical and the current behavior when printed only showed the first behavior in the p1 that it was assigned to.
image

SOLUTION FOUND (I had two variables fighting for currentBehavior)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.