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

Why don't these variables update properly?

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

✓ Accepted answer

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.

2 comments

0

Thank you!

0

No problem :)

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