SetStateEnabled throwing invalid state error when passing a state from GetState


            chosenCompanion.Humanoid:SetStateEnabled(chosenCompanion.humanoid:GetState(),false)

this line is returning this error: 18:03:53.211 - Invalid state passed to SetStateEnabled.
Anyone got any ideas?

1 Like

btw this isnt my whole script, just one line that is erroring

I’m curious; what’s the point of this?

im making a pet/egg system with companions(player models), and when I hatch an egg the player’s camera freaks out and their player falls down and moves around

1 Like

Are you confirming what’s being returned is a valid State, GetState may be returning None or nil.

My main question is why you’re setting the state to a state that is already set. If you want to lock player movement, you should be setting LocalPlayer.DevComputerMovementMode (and the touch one) to Scriptable.

This happens when I hatch an egg.

This is what I’m trying to fix.

Change the camera to Scriptable and set it’s CFrame and Focus to a specific point in world space during the hatching. If you’re scared of your character falling over, anchor it’s HumanoidRootPart during the hatching sequence

Let me show you the code.
local script

local eggOpenRemote = game:GetService("ReplicatedStorage").RemoteEvents.OnEggOpened
local Template = script:WaitForChild("Template")
local scrollingFrame = script.Parent:WaitForChild("CompanionInventory"):WaitForChild("Companions"):WaitForChild("ScrollingFrame")
local buttonConnections = {}

local function setTemplateEquipped(Template)
	for i,v in pairs(scrollingFrame:GetChildren()) do
		if v:FindFirstChild("Equipped") then
			v.Equipped.Text = "UNEQUIPPED"
			v.Equipped.TextColor3 = Color3.fromRGB(255,0,0)
		end
	end
	Template.Equipped.Text = "EQUIPPED"
	Template.Equipped.TextColor3 = Color3.fromRGB(0,255,0)
end

local function addToFrame(companion)
	local newTemplate = Template:Clone()
	newTemplate.Name = companion.Name
	newTemplate.CompanionName.Text = companion.Name
	newTemplate.Parent = scrollingFrame
	local newCompanion = companion:Clone()
	newCompanion.Parent = newTemplate.ViewportFrame
	local camera = Instance.new("Camera")
	camera.CFrame = CFrame.new(newCompanion.PrimaryPart.Position + (newCompanion.PrimaryPart.CFrame.lookVector * 3),newCompanion.PrimaryPart.Position)
	camera.Parent = newTemplate.ViewportFrame
	
	newTemplate.ViewportFrame.CurrentCamera = camera
	buttonConnections[#buttonConnections+1] = newTemplate.MouseButton1Click:Connect(function()
		if newTemplate.Equipped.Text == "EQUIPPED" then
			game.ReplicatedStorage.RemoteEvents.UnequippedCompanion:FireServer()
			newTemplate.Equipped.Text = "UNEQUIPPED"
			newTemplate.Equipped.TextColor3 = Color3.new(255,0,0)
		else
			game.ReplicatedStorage.RemoteEvents.EquipCompanion:FireServer(companion.Name)
			setTemplateEquipped(newTemplate)
		end
	end)
end


local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local camera = workspace.CurrentCamera
local eggOffsetCFrame = Instance.new("CFrameValue")  
eggOffsetCFrame.Value = CFrame.new(0,0,-5)*CFrame.Angles(0,math.rad(-180),0)  
local eggs = {}  

local shakeRotation1,shakeRotation2 = CFrame.Angles(0,0,-math.rad(45)),CFrame.Angles(0,0,math.rad(45))  

RunService.RenderStepped:Connect(function(deltaTime) 
	for egg,info in pairs(eggs) do   
		if egg:IsA("Model") then 
			egg:SetPrimaryPartCFrame(camera.CFrame:ToWorldSpace(info.offset.Value))	
		else
			egg.CFrame = camera.CFrame:ToWorldSpace(info.offset.Value) 
		end
	end
end)


function onEggOpened(egg, companion)  
	addToFrame(companion)
	coroutine.wrap(function()    
		eggs[egg] = {offset = eggOffsetCFrame:Clone()}
		local middlecframe = eggOffsetCFrame.Value  
		eggs[egg].offset.Value = middlecframe
		
		
		
		wait(0.5)	
		local shakeTweenInfo = TweenInfo.new(0.5)
		eggs[egg].shakeTween = TweenService:Create(eggs[egg].offset,shakeTweenInfo,{Value = eggs[egg].offset.Value * shakeRotation1})  
		eggs[egg].shakeTween:Play()
		
		eggs[egg].shakeTween.Completed:Wait()  
		eggs[egg].shakeTween = TweenService:Create(eggs[egg].offset,shakeTweenInfo,{Value = middlecframe * shakeRotation2})  
		eggs[egg].shakeTween:Play()
		eggs[egg].shakeTween.Completed:Wait()  
		
		wait(0.5)	
		
		
		egg:Destroy()
		
		eggs[companion] = eggs[egg]	
		eggs[egg] = nil
		eggs[companion].offset.Value = middlecframe	
		
		
		local particle = Instance.new("ParticleEmitter")
		particle.LightEmission = 1
		particle.LightInfluence = 0
		particle.ZOffset = 5
		particle.SpreadAngle = Vector2.new(0,360)
		particle.Lifetime = NumberRange.new(1)
		particle.Speed = NumberRange.new(20)
		particle.LockedToPart = true
		particle.Size = NumberSequence.new(2)
		
		particle.Enabled = false	
		if companion:IsA("Model") then particle.Parent = companion.PrimaryPart else particle.Parent = companion end
		particle:Emit(200)	
		
		companion.Parent = workspace	
		
		
		
		local player = game.Players.LocalPlayer
		local playerTextLabel = player.PlayerGui.EggOpenLabel.TextLabel
		playerTextLabel.Text = companion.Name
		playerTextLabel.Visible = true
		
		wait(2)
		
		
		playerTextLabel.Visible = false
		
		
		eggs[companion] = nil
		companion:Destroy()
	end)()
end

eggOpenRemote.OnClientEvent:Connect(function(eggmodel,companionmodel)	
	local newEggModel = eggmodel:Clone()
	

	if newEggModel:IsA("BasePart") then
		newEggModel.CanCollide = false
		newEggModel.CastShadow = false
		newEggModel.Anchored = true
	else
		for _,descendant in ipairs(newEggModel:GetDescendants()) do	
			if descendant:IsA("BasePart") then	
				descendant.CanCollide = false
				descendant.CastShadow = false
				descendant.Anchored = true
			end
		end
	end
	
	newEggModel.Parent = workspace	
	
	local newCompanionModel = companionmodel:Clone()	
	

	if newCompanionModel:IsA("BasePart") then
		newCompanionModel.CanCollide = false
		newCompanionModel.CastShadow = false
		newCompanionModel.Anchored = true
	else
		for _,descendant in ipairs(newCompanionModel:GetDescendants()) do	
			if descendant:IsA("BasePart") then	
				descendant.CanCollide = false
				descendant.CastShadow = false
				descendant.Anchored = true
			end
		end
	end
	
	onEggOpened(newEggModel, newCompanionModel)
end)

ModuleScript(Where error is)

local companionModule = {}
companionModule.companions = {
	["AlreadyPro"] = {game.ReplicatedStorage.CompanionFolder:WaitForChild("AlreadyPro")},
	["U_npluggedDev"] = {game.ReplicatedStorage.CompanionFolder:WaitForChild("U_npluggedDev")}

}
companionModule.rarities = {
	["AlreadyPro"] = 30,
	["U_npluggedDev"] = 70
}

companionModule.chooseRandomCompanion = function()
	local randomNumber = math.random(1,100)
	local counter = 0
	for rarity, weight in pairs (companionModule.rarities) do
		counter = counter + weight
		if randomNumber <= counter then
			local rarityTable = companionModule.companions[rarity]
			local chosenCompanion = rarityTable[math.random(1,#rarityTable)]

			chosenCompanion.Humanoid:SetStateEnabled(chosenCompanion.Humanoid:GetState(),false)
			return chosenCompanion
		end
	end
end
return companionModule

Script

local DataStoreService = game:GetService("DataStoreService")
local CoinsStore = DataStoreService:GetDataStore("Coins")
local RebirthsStore = DataStoreService:GetDataStore("Rebirths")

local function equipCompanion(player,companion)
	local character = player.Character
	if companion ~= nil and character ~= nil then
		if character:FindFirstChild(player.Name.."'s Companion") then character[player.Name.."'s Companion"]:Destroy() end
		if character.HumanoidRootPart:FindFirstChild("attachmentCharacter")then
			character.HumanoidRootPart:FindFirstChild("attachmentCharacter"):Destroy()
		end
		companion.Name = player.Name.."'s Companion"
		companion.pathfindingScript.Disabled = false
		
	end
end
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local Coins = Instance.new("IntValue",leaderstats)
	Coins.Name = "Coins"
	if CoinsStore:GetAsync(player.UserId) == nil then
		Coins.Value = 0
	else 
		Coins.Value = CoinsStore:GetAsync(player.UserId)
	end
	
	local Rebirths = Instance.new("IntValue",leaderstats)
	Rebirths.Name = "Rebirths"
	if RebirthsStore:GetAsync(player.UserId) == nil then
		Rebirths.Value = 0
	else
		Rebirths.Value = RebirthsStore:GetAsync(player.UserId)
	end
	local companionInventory = Instance.new("Folder",player)
	companionInventory.Name = "CompanionInventory"
	
	local equippedCompanion = Instance.new("StringValue")
	equippedCompanion.Name = "EquippedCompanion"
	equippedCompanion.Parent = player
	
	player.CharacterAdded:Connect(function(char)
		if game.ReplicatedStorage:WaitForChild("CompanionFolder"):FindFirstChild(equippedCompanion.Value)then
			equippedCompanion(player,game.ReplicatedStorage:WaitForChild("CompanionFolder"):FindFirstChild(equippedCompanion.Value):Clone())
		end
	end)
	equippedCompanion.Changed:Connect(function()
		if equippedCompanion.Value ~= nil then
			if game.ReplicatedStorage:WaitForChild("CompanionFolder"):FindFirstChild(equippedCompanion.Value)then
				equipCompanion(player,game.ReplicatedStorage:WaitForChild("CompanionFolder"):FindFirstChild(equippedCompanion.Value):Clone())
			end
		end
	end)
end)
game.ReplicatedStorage.RemoteEvents.EquipCompanion.OnServerEvent:Connect(function(player,companionName)
	local companion = game.ReplicatedStorage.CompanionFolder:FindFirstChild(companionName)
	if companion and player.CompanionInventory:FindFirstChild(companionName)then
		player.equippedCompanion.Value = companionName
		
	end
end)
game.ReplicatedStorage.RemoteEvents.UnequippedCompanion.OnServerEvent:Connect(function(player,companionName)
	player.EquippedCompanion.Value = ""
	if player.Character:FindFirstChild(player.Name.."'s Companion") then
		player.Character[player.name.."'s Companion"]:Destroy()
	end
	if player.Character.HumanoidRootPart:FindFirstChild("attachmentCharacter") then
		player.Character.HumanoidRootPart:FindFirstChild("attachmentCharacter"):Destroy(0)
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	CoinsStore:SetAsync(player.UserId,player.leaderstats.Coins.Value)
	RebirthsStore:SetAsync(player.UserId,player.leaderstats.Rebirths.Value)
end)

I guess what im trying to ask is how can I disable all State types?

not sure if it’s related to your error, but there’s a capitalization issue in the original one line you posted:

chosenCompanion.Humanoid, and then chosenCompanion.humanoid

Assuming you don’t have two humanoids in the same model, this would likely be causing some sort of issue–not sure if it’ll help though

I think you’re going about the wrong way solving this problem. Has nothing to do with the state: this is happening because the model you’re putting in front of the camera is colliding with the real player. You can quickly sidestep this problem by using a ViewportFrame or by using a CollisionGroup which isolates collisions both against itself and other groups for any camera models.

I have no idea what this is, this was made for me by a friend of mine and I only understand parts because im a backend scripter

You shouldn’t be posting here then. Your options are

  • Try fixing this yourself anyway using feedback here. Having the experience is good.
  • Ask your friend to help you if they are still available. Pass the above feedback.
  • Hiring someone to do this fix for you, though that would really just be a funds vacuum.
  • Not using this code at all. Why are you using something you’re incapable of maintaining?

I’ve presented you good workarounds - ViewportFrame and CollisionGroups. Up to you to figure out how to weave those into your systems to sidestep the problem.

Yo delete your code if you don’t need help no more. You are leaking your code dawg :sob: