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

Using variables in code further in the script before they've been defined?

Asked by 4 years ago

In my code below line 40 returns an error any time I try to run it because the variable "pName" isn't defined in the Workspace for it. How do I avoid this?

local part = script.Parent
local pName = 0
local bp = 0

function target(player)
    local pName = player.Name
    wait(1)
    local TweenService = game:GetService("TweenService")

    local part = script.Parent -- Replace with part name

    local goal = {}
    goal.Size = Vector3.new(10, 10, 10) -- Replace Vector3.new(10, 1, 10) with the size you want it to be

    local tweenInfo = TweenInfo.new(3) -- Replace 5 with how long you want the tween to be

    local tween = TweenService:Create(part, tweenInfo, goal)

    tween:Play()

    wait(3)

    local posX = game.Workspace:FindFirstChild(pName).Torso.Position.X
    local posY = game.Workspace:FindFirstChild(pName).Torso.Position.Y
    local posZ = game.Workspace:FindFirstChild(pName).Torso.Position.Z
    bp = Instance.new("BodyPosition")
    bp.Position = Vector3.new(posX, posY, posZ)
    bp.Parent = part
    bp.MaxForce = Vector3.new(400000, 400000, 400000)
end

function updater()
    local posX = game.Workspace:FindFirstChild(pName).Torso.Position.X
    local posY = game.Workspace:FindFirstChild(pName).Torso.Position.Y
    local posZ = game.Workspace:FindFirstChild(pName).Torso.Position.Z
    bp.Position = Vector3.new(posX, posY, posZ)
end

game.Players.PlayerAdded:Connect(target)
game.Workspace:FindFirstChild(pName).Torso.Changed:Connect(updater)
0
Hmm, maybe pName is just local variable at function target(player), maybe you can put that local pName above the function target. MachoMasterKirby 2 — 4y
0
Also position and cframe don't have a property changed signal which meand that you won't be able to check if the player's position has changed like you are doing it. SynthetickDev 188 — 4y
0
make it a global variable (pName = 0) DynamicDesolation 20 — 4y

Answer this question