Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is CBV not a valid member of script?

I have a local script that needs to adjust a value based on key input and that values current state. However, when the key is pressed, I get this :

17:31:25.736 - CBV is not a valid member of LocalScript
17:31:25.738 - Stack Begin
17:31:25.741 - Script 'Players.Player1.PlayerGui.ClientManager', Line 47
17:31:25.743 - Stack End

A bit further in the script, the exact same folder is referenced and used just fine. Can anyone explain this?

local player = game:GetService("Players").LocalPlayer 
local MovementStyle
local IdleStyle
local MovementAsset = {"rbxassetid://643347794","rbxassetid://643348089"}
local Mouse = player:GetMouse()
local Target = Mouse.Target
local CBV = player.PlayerGui.ClientManager:FindFirstChild("CombatValues")
if CBV ~= nil then print("Data Located")
end
local MotionEvent = game.ReplicatedStorage:WaitForChild("MotionEvent")
local InteractEvent = game.ReplicatedStorage:WaitForChild("Interact")

function onKeyPress(inputObject, gameProcessedEvent)
		if inputObject.KeyCode == Enum.KeyCode.W or inputObject.KeyCode == Enum.KeyCode.S or inputObject.KeyCode == Enum.KeyCode.A or inputObject.KeyCode == Enum.KeyCode.D 
		and script.CharacterState.Value == "Exploration" and script.Sprinting.Value == false then 
		MovementStyle = ("rbxassetid://661645004")
		IdleStyle = ("rbxassetid://661054611")
			MotionEvent:FireServer(MovementStyle,IdleStyle) 
			print(MovementStyle)
			
			
			
		elseif inputObject.KeyCode == Enum.KeyCode.F and script.CharacterState.Value == "Exploration" then
			script.CharacterState.Value = "Combative"
		elseif inputObject.KeyCode == Enum.KeyCode.F and script.CharacterState.Value == "Combative" then
			script.CharacterState.Value = "Exploration"
			--InteractEvent:FireServer(Target.Parent,Target.Parent.Name)
		elseif inputObject.KeyCode == Enum.KeyCode.LeftShift and script.Sprinting.Value == true then
			script.Sprinting.Value = false
			elseif inputObject.KeyCode == Enum.KeyCode.LeftShift and script.Sprinting.Value == false then
			script.Sprinting.Value = true
			--print("Sprintinging Toggled!")
----------------------The Problem Area------------------------------------
			elseif inputObject.KeyCode == Enum.KeyCode.H and script.CBV.AttackDirection.Value ~= "Central" then
			script.AttackDirection.Value = "Central"
			elseif inputObject.KeyCode == Enum.KeyCode.G and script.CBV.AttackDirection.Value == "Left" then
			script.AttackDirection.Value = "Right"
			elseif inputObject.KeyCode == Enum.KeyCode.G and script.CBV.AttackDirection.Value == "Right" then
			script.AttackDirection.Value = "Left"
	end
end

--IMPORTANT-- Switching attack directions resets attack cooldown resulting in faster combat

game:GetService("UserInputService").InputBegan:connect(onKeyPress)

Mouse.Button1Down:connect(function()
if CBV.AttackCoolDown.Value == 0 and CBV.Swinging.Value == false then
	CBV.AttackCoolDown.Value = 1
	print("Attacking")
	
end
end)

1 answer

✓ Accepted answer

The difference between your problem area and the last couple lines is the way you're attempting to access CBV. When dealing with the client itself and its descendants, you have to get familiar with using :WaitForChild(). In this case, you set the CBV variable using :FindFirstChild() which substitutes for :WaitForChild(). In your problem area, you directly said script.CBV and there's a chance that the script doesn't exactly see the specified Instance yet.

To fix your problem, change all of the 'problem' lines' way of accessing the Instance to using the variable. If that was a bit confusing, change lines that use

script.CBV.AttackDirection.Value

to

CBV.AttackDirection.Value

Hope I've helped.

This discussion is preserved as it was. New answers, comments, and votes are closed.