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

Why don't these variables update properly?

Asked by 7 years ago
Edited 7 years ago

I have two variables, done and triggered. Both update momentarily as they are updated in the code, but immediately after they are reset. Any ideas on what's happening? I've already tried making them their own separate values, but the same thing happens.

local parPar = script.Parent.Parent

local posX = parPar.Position.X
local posY = parPar.Position.Y
local posZ = parPar.Position.Z

local done = false
local triggered = false

local function timer()
    wait(1.5)
    local done = true
end

script.Parent.Touched:connect(function(hit)
    if not triggered then
        if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
            if hit.Name == "Head" or hit.Parent.ClassName == "Accessory" and hit.Parent.humanoid.Jump == true and triggered == false then
                local triggered = true
                parPar.CanCollide = false
                local head = hit.Parent.Head
                local headPosY = head.Position.Y
                while done == false do
                    local oldHeadPosY = headPosY
                    local headPosY = head.Position.Y
                    local difference = headPosY - oldHeadPosY
                    local sum = posY + difference
                    parPar.Position = Vector3.new(posX, sum, posZ)
                    wait()
                end
                parPar.Position = Vector3.new(posX, posY, posZ)
                parPar.CanCollide = true
            end
        end
    end
end)

EDIT: Forgot to mention that no errors come up in the output.

1 answer

Log in to vote
0
Answered by 7 years ago

Your problem is you are not actually updating the variables you want to update. More specifically, you are using "local" in timer() to set done equal to true. This is creating a new variable called done only accessible in that scope. Same with your triggered variable, you are just re creating it.

All you have to do is just remove the local from line 12 and 19, it will instead just change the variable you had already defined instead of defining a new one for that scope.

It is "resetting" itself, because after that code in connect() and timer() are finished, it is still using the other variables you made at the beginning of the script because the other variables are now out of scope.

Hope this helped.

0
Thank you! AvatarAang400 35 — 7y
0
No problem :) Gamprofwiesonel 31 — 7y
Ad

Answer this question