Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

I cannot get code to recognize a stringvalue?

Asked by
zub74 0
10 years ago

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

Log in to vote
2
Answered by 10 years ago

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")

Ad

Answer this question