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

I cannot get code to recognize a stringvalue?

For some reason, I cannot get this to work. I error out, says Pos is not a valid member of Player when it clearly is.

if not script.Parent.Parent:FindFirstChild("Pos") then
	wait()
end


local Direction=script.Parent.Parent.Direction
local Pos=script.Parent.Parent.Pos
local Pval=Pos.Value

Direction.Changed:connect(function(Direction)
	if Direction=="Up" then
		Pval=Pval+Vector3.new(0,0,2)
	end
	if Direction=="Down" then
		Pval=Pval+Vector3.new(0,0,-2)
	end
	if Direction=="Left" then
		Pval=Pval+Vector3.new(-2,0,0)
	end
	if Direction=="Right" then
		Pval=Pval+Vector3.new(2,0,0)
	end
end)

I have also tried this, also a no go.

if not script.Parent.Parent:FindFirstChild("Pos") then
	Pos= Instance.new("StringValue", script.Parent.Parent)
	Pos.Name="Pos"
end


local Direction=script.Parent.Parent.Direction

Direction.Changed:connect(function(Direction)
	if Direction=="Up" then
		Pos.Value=Pos.Value+Vector3.new(0,0,2)
	end
	if Direction=="Down" then
		Pos.Value=Pos.Value+Vector3.new(0,0,-2)
	end
	if Direction=="Left" then
		Pos.Value=Pos.Value+Vector3.new(-2,0,0)
	end
	if Direction=="Right" then
		Pos.Value=Pos.Value+Vector3.new(2,0,0)
	end
end)

1 answer

What's inside an if block only gets executed once, so you need a while block, which keeps checking the condition until it's true:

while not script.Parent.Parent:FindFirstChild("Pos") then wait() end

But really you should just use the WaitForChild method:

local Pos=script.Parent.Parent:WaitForChild("Pos")

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