Variable Randomly Changes

  1. What do you want to achieve? Not make the variable change without me wanting it to.

  2. What is the issue? The variable randomly changes for no reason.

  3. What solutions have you tried so far? Cannot find anything anywhere. I’ve surfed the web.

	local Object = self.Object
	local DragInput	= nil
	local DragStart	= nil
	local StartPosition = nil
	local PreparingToDrag = false

	local function Update(Input)
		local Delta = Input.Position - DragStart
		TweenService:Create(self.Object, TweenInfo.new(DragSpeed), {Position = UDim2.new(StartPosition.X.Scale, StartPosition.X.Offset + Delta.X, StartPosition.Y.Scale, StartPosition.Y.Offset + Delta.Y)}):Play()
	end

	self.InputBegan = Object.InputBegan:Connect(function(Input)
		if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
			if not CurrentlyDragging then
				print("Began")
				CurrentlyDragging = true
				DragStart = Input.Position
				StartPosition = Object.Position
				print(DragStart) -- prints a Vector3
				print(StartPosition) -- prints a UDim2
			end
		end
	end)

	self.InputChanged = UserInputService.InputChanged:Connect(function(Input, GPE)
		if GPE then
			return
		end
		if Input.UserInputType == Enum.UserInputType.MouseMovement or Input.UserInputType == Enum.UserInputType.Touch then
			if CurrentlyDragging then
				print(DragStart) -- prints nil
				print(StartPosition) -- prints nil
				Update(Input)
			end
		end
	end)
	
	self.InputEnded = Object.InputEnded:Connect(function(Input)
		if Input.UserInputType == Enum.UserInputType.MouseButton1 or Input.UserInputType == Enum.UserInputType.Touch then
			print("Ended")
			print(DragStart) -- prints a Vector3
			print(StartPosition) -- prints a UDim2
			CurrentlyDragging = false
			print(DragStart) -- prints nil
			print(StartPosition) -- prints nil
		end
	end)

Any reason for this? This isn’t the full code, but it is the relevant areas, and the only area where the variables change.

Nothing changes for no reason unless you’re talking about theoretical physics beyond the standard model, which even then is all about interpretation. There’s clearly some code responsible for changing the variables:

It’s just a matter of finding out when and why those codes execute. Try using the debug tool built in for Studio, specifically the breakpoints and variables watch.